KVM TDX basic feature support
Subsystems (8)
Re: [RFC PATCH v5 037/104] KVM: x86/mmu: Allow non-zero init value for shadow PTE
In reply to [email protected]Quoted 5 lines · On Fri, 2022-03-04 at 11:48 -0800, [email protected] wrote:
From: Sean Christopherson <[email protected]>
TDX will run with EPT violation #VEs enabled for shared EPT, which means KVM needs to set the "suppress #VE" bit in unused PTEs to avoid unintentionally reflecting not-present EPT violations into the guest.
This sentence is hard to interpret. Please add more sentences to elaborate "TDX will run with EPT violation #VEs enabled for shared EPT". Also, this patch is the first time to introduce "shared EPT", perhaps you should also explain it here. Or even you can move patch 43 ("KVM: TDX: Add load_mmu_pgd method for TDX") before this one.
"reflecting non-present EPT violations into the guest" could be hard to interpret. Perhaps you can be more explicit to say VMM wants to get EPT violation for normal (shared) memory access rather than to cause #VE to guest.
Mentioning you want EPT violation instead of #VE for normal (shared) memory access also completes your statement of wanting #VE for MMIO below, so that people can have a clear picture when to get a #VE when not.
Quoted 5 lines
Because guest memory is protected with TDX, VMM can't parse instructions in the guest memory. Instead, MMIO hypercall is used to pass necessary information to VMM.
Quoted 7 lines
To make unmodified device driver work, guest TD expects #VE on accessing shared GPA. The #VE handler converts MMIO access into MMIO hypercall with the EPT entry of enabled "#VE" by clearing "suppress #VE" bit. Before VMM enabling #VE, it needs to figure out the given GPA is for MMIO by EPT violation. So the execution flow looks like
- allocate unused shared EPT entry with suppress #VE bit set.
allocate -> Allocate
Quoted 4 lines
- EPT violation on that GPA.
- VMM figures out the faulted GPA is for MMIO.
- VMM clears the suppress #VE bit.
- Guest TD gets #VE, and converts MMIO access into MMIO hypercall.
Here you have described both normal memory access and MMIO, it's good time to summarize the purpose of this patch: For both cases you want PTE with "suppress #VE" bit set initially when it is allocated, therefore allow non-zero init value for PTE.
Quoted 27 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 50 +++++++++++++++++++++++++++++++++++------ arch/x86/kvm/mmu/spte.c | 10 +++++++++ arch/x86/kvm/mmu/spte.h | 2 ++ 4 files changed, 56 insertions(+), 7 deletions(-)arch/x86/kvm/mmu.h +1
-0@@ -66,6 +66,7 @@ static __always_inline u64 rsvd_bits(int s, int e)void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask);void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only); +void kvm_mmu_set_spte_init_value (u64 init_value);void kvm_init_mmu (struct kvm_vcpu *vcpu); void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, unsigned long cr0,arch/x86/kvm/mmu/mmu.c +0
-0
arch/x86/kvm/mmu/mmu.c static int mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep) · line 622
Quoted 8 lines
arch/x86/kvm/mmu/mmu.c +2
-2@@ -617,9 +617,9 @@ static int mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep)int level =sptep_to_sp (sptep)->role.level;if (!spte_has_volatile_bits (old_spte)) -__update_clear_spte_fast (sptep,0ull ); +__update_clear_spte_fast (sptep, shadow_init_value);else - old_spte =__update_clear_spte_slow (sptep,0ull ); + old_spte =__update_clear_spte_slow (sptep, shadow_init_value);
I guess it's better to have some comment here. Allow non-zero init value for shadow PTE doesn't necessarily mean the initial value should be used when one PTE is zapped. I think mmu_spte_clear_track_bits() is only called for mapping of normal (shared) memory but not MMIO? Then perhaps it's better to have a comment to explain we want "suppress #VE" set to get a real EPT violation for normal memory access from guest?
arch/x86/kvm/mmu/mmu.c static int mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep) · line 655
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +1
-1if (!is_shadow_present_pte (old_spte))return old_spte;@@ -651,7 +651,7 @@ static int mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep)*/ static void mmu_spte_clear_no_track (u64 *sptep) { -__update_clear_spte_fast (sptep,0ull ); +__update_clear_spte_fast (sptep, shadow_init_value); }
Similar here. Seems mmu_spte_clear_no_track() is used to zap non-leaf PTE which doesn't require state tracking, so theoretically it can be set to 0. But this seems is also called to zap MMIO PTE so looks need to set to shadow_init_value. Anyway looks deserve a comment?
Btw, Above two changes to mmu_spte_clear_track_bits() and mmu_spte_clear_track_bits() seems a little bit out-of-scope of what this patch claims to do. Allow non-zero init value for shadow PTE doesn't necessarily mean the initial value should be used when one PTE is zapped. Maybe we can further improve the patch title and commit message a little bit. Such as: Allow non- zero value for empty (or invalid?) PTE? Non-present seems doesn't fit here.
Quoted 56 lines
arch/x86/kvm/mmu/mmu.c +37
-2static u64 mmu_spte_get_lockless (u64 *sptep)@@ -737,6 +737,42 @@ static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu) } } +static inline void kvm_init_shadow_page (void *page) +{ +#ifdef CONFIG_X86_64 +int ign; + + asmvolatile ( +"rep stosq\n\t" + :"=c" (ign),"=D" (page) + :"a" (shadow_init_value),"c" (4096 /8 ),"D" (page) + :"memory" + ); +#else +BUG (); +#endif +} + +static int mmu_topup_shadow_page_cache (struct kvm_vcpu *vcpu) +{ +struct kvm_mmu_memory_cache *mc = &vcpu->arch.mmu_shadow_page_cache; +int start, end, i, r; + +if (shadow_init_value) + start =kvm_mmu_memory_cache_nr_free_objects (mc); + + r =kvm_mmu_topup_memory_cache (mc,PT64_ROOT_MAX_LEVEL ); +if (r) +return r; + +if (shadow_init_value) { + end =kvm_mmu_memory_cache_nr_free_objects (mc); +for (i = start; i < end; i++) +kvm_init_shadow_page (mc->objects[i]); + } +return 0 ; +} +static int mmu_topup_memory_caches (struct kvm_vcpu *vcpu,bool maybe_indirect) {int r;@@ -746,8 +782,7 @@ static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu, bool maybe_indirect) 1 + PT64_ROOT_MAX_LEVEL + PTE_PREFETCH_NUM);if (r)return r; - r =kvm_mmu_topup_memory_cache (&vcpu->arch.mmu_shadow_page_cache, -PT64_ROOT_MAX_LEVEL ); + r =mmu_topup_shadow_page_cache (vcpu);if (r)return r;if (maybe_indirect) {
arch/x86/kvm/mmu/mmu.c static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 3184
Quoted 5 lines
arch/x86/kvm/mmu/mmu.c +1
-1@@ -3146,7 +3181,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) {struct kvm_mmu_page *sp;int ret =RET_PF_INVALID ; -u64 spte =0ull ; +u64 spte = shadow_init_value;
I don't quite understand this change. 'spte' is set to the last level PTE of the given GFN if mapping is found. Otherwise fast_page_fault() returns RET_PF_INVALID. In both cases, the initial value doesn't matter.
Am I wrong?
Quoted 61 lines
arch/x86/kvm/mmu/mmu.c +2
-1u64 *sptep =NULL ;uint retry_count =0 ;@@ -5598,7 +5633,8 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu) vcpu->arch.mmu_page_header_cache.kmem_cache = mmu_page_header_cache; vcpu->arch.mmu_page_header_cache.gfp_zero = __GFP_ZERO; - vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; +if (!shadow_init_value) + vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; vcpu->arch.mmu = &vcpu->arch.root_mmu; vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;arch/x86/kvm/mmu/spte.c +10
-0@@ -35,6 +35,7 @@ u64 __read_mostly shadow_mmio_access_mask;u64 __read_mostly shadow_present_mask;u64 __read_mostly shadow_me_mask;u64 __read_mostly shadow_acc_track_mask; +u64 __read_mostly shadow_init_value;u64 __read_mostly shadow_nonpresent_or_rsvd_mask;u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask;@@ -223,6 +224,14 @@ u64 kvm_mmu_changed_pte_notifier_make_spte(u64 old_spte, kvm_pfn_t new_pfn)return new_spte; } +void kvm_mmu_set_spte_init_value (u64 init_value) +{ +if (WARN_ON (!IS_ENABLED (CONFIG_X86_64 ) && init_value)) + init_value =0 ; + shadow_init_value = init_value; +} +EXPORT_SYMBOL_GPL (kvm_mmu_set_spte_init_value); +static u8 kvm_get_shadow_phys_bits (void ) {/* @@ -367,6 +376,7 @@ void kvm_mmu_reset_all_pte_masks(void) shadow_present_mask =PT_PRESENT_MASK ; shadow_acc_track_mask =0 ; shadow_me_mask = sme_me_mask; + shadow_init_value =0 ; shadow_host_writable_mask =DEFAULT_SPTE_HOST_WRITEABLE ; shadow_mmu_writable_mask =DEFAULT_SPTE_MMU_WRITEABLE ;arch/x86/kvm/mmu/spte.h +2
-0@@ -171,6 +171,8 @@ extern u64 __read_mostly shadow_mmio_access_mask;extern u64 __read_mostly shadow_present_mask;extern u64 __read_mostly shadow_me_mask; +extern u64 __read_mostly shadow_init_value; +/* * SPTEs in MMUs without A/D bits are marked with SPTE_TDP_AD_DISABLED_MASK; * shadow_acc_track_mask is the set of bits to be cleared in non-accessed
Re: [RFC PATCH v5 037/104] KVM: x86/mmu: Allow non-zero init value for shadow PTE
Quoted 12 lines · Kai Huang
Quoted 12 lines
arch/x86/kvm/mmu/mmu.c +0
-0@@ -617,9 +617,9 @@ static int mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep) int level = sptep_to_sp(sptep)->role.level;
if (!spte_has_volatile_bits(old_spte))arch/x86/kvm/mmu/mmu.c +1
-1-__update_clear_spte_fast (sptep,0ull ); +__update_clear_spte_fast (sptep, shadow_init_value);else
arch/x86/kvm/mmu/mmu.c +1
-1- old_spte =__update_clear_spte_slow (sptep,0ull ); + old_spte = __update_clear_spte_slow(sptep,
Quoted 8 lines
Quoted 1 line
shadow_init_value);I guess it's better to have some comment here. Allow non-zero init value for shadow PTE doesn't necessarily mean the initial value should be used when one PTE is zapped. I think
mmu_spte_clear_track_bits()is only called for mapping of normal (shared) memory but not MMIO? Then perhaps it's better to have a comment to explain we want "suppress #VE" set to get a real EPT violation for normal memory access from guest?
Btw, I think the relevant part of TDP MMU change should be included in this patch too otherwise TDP MMU is broken with this patch.
Actually in this series legacy MMU is not supported to work with TDX, so above change to legacy MMU doesn't matter actually. Instead, TDP MMU change should be here.
Signature · 2 lines
Thanks, -Kai
Paolo Bonzini
maintainerRe: [RFC PATCH v5 037/104] KVM: x86/mmu: Allow non-zero init value for shadow PTE
Quoted 2 lines · Kai Huang
Btw, I think the relevant part of TDP MMU change should be included in this patch too otherwise TDP MMU is broken with this patch.
I agree.
Paolo
Quoted 3 lines
Actually in this series legacy MMU is not supported to work with TDX, so above change to legacy MMU doesn't matter actually. Instead, TDP MMU change should be here.
Re: [RFC PATCH v5 037/104] KVM: x86/mmu: Allow non-zero init value for shadow PTE
Quoted 11 lines · Paolo Bonzini
Quoted 2 lines · Kai Huang
Btw, I think the relevant part of TDP MMU change should be included in this patch too otherwise TDP MMU is broken with this patch.
I agree.
Paolo
Quoted 3 lines
Actually in this series legacy MMU is not supported to work with TDX, so above change to legacy MMU doesn't matter actually. Instead, TDP MMU change should be here.
Sure, will reorganize it in the next respin.
Signature · 1 line
Isaku Yamahata <[email protected]>
Paolo Bonzini
maintainerRe: [RFC PATCH v5 037/104] KVM: x86/mmu: Allow non-zero init value for shadow PTE
In reply to Kai HuangQuoted 9 lines · Kai Huang
arch/x86/kvm/mmu/mmu.c static int mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep) · line 622
Quoted 9 lines
arch/x86/kvm/mmu/mmu.c +2
-2@@ -617,9 +617,9 @@ static int mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep)int level =sptep_to_sp (sptep)->role.level;if (!spte_has_volatile_bits (old_spte)) -__update_clear_spte_fast (sptep,0ull ); +__update_clear_spte_fast (sptep, shadow_init_value);else - old_spte =__update_clear_spte_slow (sptep,0ull ); + old_spte =__update_clear_spte_slow (sptep, shadow_init_value);
(FWIW this one should also assume that the init_value is zero, and WARN if not).
Quoted 22 lines
I guess it's better to have some comment here. Allow non-zero init value for shadow PTE doesn't necessarily mean the initial value should be used when one PTE is zapped. I think
mmu_spte_clear_track_bits()is only called for mapping of normal (shared) memory but not MMIO? Then perhaps it's better to have a comment to explain we want "suppress #VE" set to get a real EPT violation for normal memory access from guest?arch/x86/kvm/mmu/mmu.c static int mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep) · line 655
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +1
-1if (!is_shadow_present_pte (old_spte))return old_spte;@@ -651,7 +651,7 @@ static int mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep)*/ static void mmu_spte_clear_no_track (u64 *sptep) { -__update_clear_spte_fast (sptep,0ull ); +__update_clear_spte_fast (sptep, shadow_init_value); }Similar here. Seems
mmu_spte_clear_no_track()is used to zap non-leaf PTE which doesn't require state tracking, so theoretically it can be set to 0. But this seems is also called to zap MMIO PTE so looks need to set toshadow_init_value. Anyway looks deserve a comment?
Quoted 6 lines
Btw, Above two changes to
mmu_spte_clear_track_bits()andmmu_spte_clear_track_bits()seems a little bit out-of-scope of what this patch claims to do. Allow non-zero init value for shadow PTE doesn't necessarily mean the initial value should be used when one PTE is zapped. Maybe we can further improve the patch title and commit message a little bit. Such as: Allow non- zero value for empty (or invalid?) PTE? Non-present seems doesn't fit here.
I think shadow_init_value is not named well. Let's rename it to shadow_nonpresent_value, and the commit to "Allow non-zero value for non-present SPTE". That explains why it's used for zapping.
Paolo
Re: [RFC PATCH v5 037/104] KVM: x86/mmu: Allow non-zero init value for shadow PTE
In reply to [email protected]arch/x86/kvm/mmu/mmu.c static int mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep) · line 655
Quoted 6 lines · On 3/4/22 20:48, [email protected] wrote:
static void mmu_spte_clear_no_track(u64 *sptep) {arch/x86/kvm/mmu/mmu.c +1
-1-__update_clear_spte_fast (sptep,0ull ); +__update_clear_spte_fast (sptep, shadow_init_value); }
Please WARN_ON_ONCE if shadow_init_value is nonzero, and then keep 0ull as the argument.
I have not thought much of the steps that are needed if were to flip both bit 0 and bit 63, so let's at least document that with a WARN.
Otherwise,
Paolo
RFC PATCH v5 38/104 KVM: x86/mmu: Allow per-VM override of the TDP max page level
In reply to [email protected]From: Sean Christopherson <[email protected]>
In the existing x86 KVM MMU code, there is already max_level member in struct kvm_page_fault with KVM_MAX_HUGEPAGE_LEVEL initial value. The KVM page fault handler denies page size larger than max_level.
Add per-VM member to indicate the allowed maximum page size with KVM_MAX_HUGEPAGE_LEVEL as default value and initialize max_level in struct kvm_page_fault with it.
For the guest TD, the set per-VM value for allows maximum page size to 4K page size. Then only allowed page size is 4K. It means large page is disabled.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu.h | 2 +- arch/x86/kvm/mmu/mmu.c | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1053,6 +1053,7 @@ struct kvm_arch {unsigned long n_requested_mmu_pages;unsigned long n_max_mmu_pages;unsigned int indirect_shadow_pages; +int tdp_max_page_level;u8 mmu_valid_gen;struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES ];struct list_head active_mmu_pages;
arch/x86/kvm/mmu.h +1 -1
@@ -192,7 +192,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(), - .max_level = KVM_MAX_HUGEPAGE_LEVEL, + .max_level = vcpu->kvm->arch.tdp_max_page_level, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, };
arch/x86/kvm/mmu/mmu.c +2 -0
@@ -5782,6 +5782,8 @@ void kvm_mmu_init_vm(struct kvm *kvm) node->track_write = kvm_mmu_pte_write; node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot;kvm_page_track_register_notifier (kvm, node); + + kvm->arch.tdp_max_page_level =KVM_MAX_HUGEPAGE_LEVEL ; }void kvm_mmu_uninit_vm (struct kvm *kvm)
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 038/104] KVM: x86/mmu: Allow per-VM override of the TDP max page level
Quoted 6 lines · On Fri, 2022-03-04 at 11:48 -0800, [email protected] wrote:
From: Sean Christopherson <[email protected]>
In the existing x86 KVM MMU code, there is already
max_levelmember in structkvm_page_faultwithKVM_MAX_HUGEPAGE_LEVELinitial value. The KVM page fault handler denies page size larger thanmax_level.
Quoted 7 lines
Add per-VM member to indicate the allowed maximum page size with
KVM_MAX_HUGEPAGE_LEVELas default value and initializemax_levelin structkvm_page_faultwith it.For the guest TD, the set per-VM value for allows maximum page size to 4K page size. Then only allowed page size is 4K. It means large page is disabled.
Do not support large page for TD is the reason that you want this change, but not the result. Please refine a little bit.
Quoted 47 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu.h | 2 +- arch/x86/kvm/mmu/mmu.c | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -1053,6 +1053,7 @@ struct kvm_arch {unsigned long n_requested_mmu_pages;unsigned long n_max_mmu_pages;unsigned int indirect_shadow_pages; +int tdp_max_page_level;u8 mmu_valid_gen;struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES ];struct list_head active_mmu_pages;arch/x86/kvm/mmu.h +1
-1@@ -192,7 +192,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(), - .max_level = KVM_MAX_HUGEPAGE_LEVEL, + .max_level = vcpu->kvm->arch.tdp_max_page_level, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, };arch/x86/kvm/mmu/mmu.c +2
-0@@ -5782,6 +5782,8 @@ void kvm_mmu_init_vm(struct kvm *kvm) node->track_write = kvm_mmu_pte_write; node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot;kvm_page_track_register_notifier (kvm, node); + + kvm->arch.tdp_max_page_level =KVM_MAX_HUGEPAGE_LEVEL ; }void kvm_mmu_uninit_vm (struct kvm *kvm)
Sean Christopherson
maintainerRe: [RFC PATCH v5 038/104] KVM: x86/mmu: Allow per-VM override of the TDP max page level
Quoted 11 lines · Kai Huang
Quoted 10 lines · On Fri, 2022-03-04 at 11:48 -0800, [email protected] wrote:
From: Sean Christopherson <[email protected]>
In the existing x86 KVM MMU code, there is already
max_levelmember in structkvm_page_faultwithKVM_MAX_HUGEPAGE_LEVELinitial value. The KVM page fault handler denies page size larger thanmax_level.Add per-VM member to indicate the allowed maximum page size with
KVM_MAX_HUGEPAGE_LEVELas default value and initializemax_levelin structkvm_page_faultwith it.
Quoted 6 lines
Quoted 3 lines
For the guest TD, the set per-VM value for allows maximum page size to 4K page size. Then only allowed page size is 4K. It means large page is disabled.
Do not support large page for TD is the reason that you want this change, but not the result. Please refine a little bit.
Not supporting huge pages was fine for the PoC, but I'd prefer not to merge TDX without support for huge pages. Has any work been put into enabling huge pages? If so, what's the technical blocker? If not...
Re: [RFC PATCH v5 038/104] KVM: x86/mmu: Allow per-VM override of the TDP max page level
Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Kai Huang
Quoted 13 lines · On Fri, 2022-03-04 at 11:48 -0800, [email protected] wrote:
From: Sean Christopherson <[email protected]>
In the existing x86 KVM MMU code, there is already
max_levelmember in structkvm_page_faultwithKVM_MAX_HUGEPAGE_LEVELinitial value. The KVM page fault handler denies page size larger thanmax_level.Add per-VM member to indicate the allowed maximum page size with
KVM_MAX_HUGEPAGE_LEVELas default value and initializemax_levelin structkvm_page_faultwith it.For the guest TD, the set per-VM value for allows maximum page size to 4K page size. Then only allowed page size is 4K. It means large page is disabled.
Quoted 6 lines
Quoted 2 lines
Do not support large page for TD is the reason that you want this change, but not the result. Please refine a little bit.
Not supporting huge pages was fine for the PoC, but I'd prefer not to merge TDX without support for huge pages. Has any work been put into enabling huge pages? If so, what's the technical blocker? If not...
I wanted to get feedback on the approach (always set SPTE to REMOVED_SPTE, callback, set the SPTE to the final value instead of relying atomic update SPTE) before going further for large page.
Signature · 1 line
Isaku Yamahata <[email protected]>
Sean Christopherson
maintainerRe: [RFC PATCH v5 038/104] KVM: x86/mmu: Allow per-VM override of the TDP max page level
Quoted 22 lines · Isaku Yamahata
Quoted 19 lines · Sean Christopherson
Quoted 17 lines · Kai Huang
Quoted 6 lines · On Fri, 2022-03-04 at 11:48 -0800, [email protected] wrote:
From: Sean Christopherson <[email protected]>
In the existing x86 KVM MMU code, there is already
max_levelmember in structkvm_page_faultwithKVM_MAX_HUGEPAGE_LEVELinitial value. The KVM page fault handler denies page size larger thanmax_level.Quoted 7 lines
Add per-VM member to indicate the allowed maximum page size with
KVM_MAX_HUGEPAGE_LEVELas default value and initializemax_levelin structkvm_page_faultwith it.For the guest TD, the set per-VM value for allows maximum page size to 4K page size. Then only allowed page size is 4K. It means large page is disabled.
Do not support large page for TD is the reason that you want this change, but not the result. Please refine a little bit.
Quoted 7 lines
Quoted 3 lines
Not supporting huge pages was fine for the PoC, but I'd prefer not to merge TDX without support for huge pages. Has any work been put into enabling huge pages? If so, what's the technical blocker? If not...
I wanted to get feedback on the approach (always set SPTE to
REMOVED_SPTE, callback, set the SPTE to the final value instead of relying atomic update SPTE) before going further for large page.
Pretty please with a cherry on top, send an email calling out which areas and patches you'd like "immediate" feedback on. Putting that information in the cover letter would have been extremely helpful. I realize it's hard to balance providing context for folks who don't know TDX with "instructions" for reviewers, but one of the most helpful things you can do for reviewers is to make it explicitly clear
what _your_ expectations and wants are, _why_ you posted the series. Usually that
information is implied, i.e. you want your patches merged, but that's obviously not the case here.
Re: [RFC PATCH v5 038/104] KVM: x86/mmu: Allow per-VM override of the TDP max page level
In reply to Sean ChristophersonQuoted 16 lines · Sean Christopherson
Quoted 15 lines · Kai Huang
Quoted 13 lines · On Fri, 2022-03-04 at 11:48 -0800, [email protected] wrote:
From: Sean Christopherson <[email protected]>
In the existing x86 KVM MMU code, there is already
max_levelmember in structkvm_page_faultwithKVM_MAX_HUGEPAGE_LEVELinitial value. The KVM page fault handler denies page size larger thanmax_level.Add per-VM member to indicate the allowed maximum page size with
KVM_MAX_HUGEPAGE_LEVELas default value and initializemax_levelin structkvm_page_faultwith it.For the guest TD, the set per-VM value for allows maximum page size to 4K page size. Then only allowed page size is 4K. It means large page is disabled.
Quoted 6 lines
Quoted 2 lines
Do not support large page for TD is the reason that you want this change, but not the result. Please refine a little bit.
Not supporting huge pages was fine for the PoC, but I'd prefer not to merge TDX without support for huge pages. Has any work been put into enabling huge pages? If so, what's the technical blocker? If not...
Hi Sean,
Is there any reason large page support must be included in the initial merge of TDX? Large page is more about performance improvement I think. Given this series is already very big, perhaps we can do it later.
Signature · 2 lines
Thanks, -Kai
Sean Christopherson
maintainerRe: [RFC PATCH v5 038/104] KVM: x86/mmu: Allow per-VM override of the TDP max page level
Quoted 24 lines · Kai Huang
Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Kai Huang
Quoted 13 lines · On Fri, 2022-03-04 at 11:48 -0800, [email protected] wrote:
From: Sean Christopherson <[email protected]>
In the existing x86 KVM MMU code, there is already
max_levelmember in structkvm_page_faultwithKVM_MAX_HUGEPAGE_LEVELinitial value. The KVM page fault handler denies page size larger thanmax_level.Add per-VM member to indicate the allowed maximum page size with
KVM_MAX_HUGEPAGE_LEVELas default value and initializemax_levelin structkvm_page_faultwith it.For the guest TD, the set per-VM value for allows maximum page size to 4K page size. Then only allowed page size is 4K. It means large page is disabled.
Quoted 6 lines
Quoted 2 lines
Do not support large page for TD is the reason that you want this change, but not the result. Please refine a little bit.
Not supporting huge pages was fine for the PoC, but I'd prefer not to merge TDX without support for huge pages. Has any work been put into enabling huge pages? If so, what's the technical blocker? If not...
Quoted 5 lines
Hi Sean,
Is there any reason large page support must be included in the initial merge of TDX? Large page is more about performance improvement I think. Given this series is already very big, perhaps we can do it later.
I'm ok punting 1gb for now, but I want to have a high level of confidence that 2mb pages will work without requiring significant churn in KVM on top of the initial TDX support. I suspect gaining that level of confidence will mean getting 95%+ of the way to a fully working code base. IIRC, 2mb wasn't expected to be terrible, it was 1gb support where things started to get messy.
Re: [RFC PATCH v5 038/104] KVM: x86/mmu: Allow per-VM override of the TDP max page level
Quoted 31 lines · Sean Christopherson
Quoted 24 lines · Kai Huang
Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Kai Huang
Quoted 13 lines · On Fri, 2022-03-04 at 11:48 -0800, [email protected] wrote:
From: Sean Christopherson <[email protected]>
In the existing x86 KVM MMU code, there is already
max_levelmember in structkvm_page_faultwithKVM_MAX_HUGEPAGE_LEVELinitial value. The KVM page fault handler denies page size larger thanmax_level.Add per-VM member to indicate the allowed maximum page size with
KVM_MAX_HUGEPAGE_LEVELas default value and initializemax_levelin structkvm_page_faultwith it.For the guest TD, the set per-VM value for allows maximum page size to 4K page size. Then only allowed page size is 4K. It means large page is disabled.
Quoted 6 lines
Quoted 2 lines
Do not support large page for TD is the reason that you want this change, but not the result. Please refine a little bit.
Not supporting huge pages was fine for the PoC, but I'd prefer not to merge TDX without support for huge pages. Has any work been put into enabling huge pages? If so, what's the technical blocker? If not...
Quoted 5 lines
Hi Sean,
Is there any reason large page support must be included in the initial merge of TDX? Large page is more about performance improvement I think. Given this series is already very big, perhaps we can do it later.
Quoted 5 lines
I'm ok punting 1gb for now, but I want to have a high level of confidence that 2mb pages will work without requiring significant churn in KVM on top of the initial TDX support. I suspect gaining that level of confidence will mean getting 95%+ of the way to a fully working code base. IIRC, 2mb wasn't expected to be terrible, it was 1gb support where things started to get messy.
OK no argument here :)
Signature · 2 lines
Thanks, -Kai
RFC PATCH v5 39/104 KVM: x86/mmu: Disallow fast page fault on private GPA
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX requires TDX SEAMCALL to operate Secure EPT instead of direct memory access and TDX SEAMCALL is heavy operation. Fast page fault on private GPA doesn't make sense. Disallow fast page fault on private GPA.
--- arch/x86/kvm/mmu/mmu.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/kvm/mmu/mmu.c +7 -0
@@ -3185,6 +3185,13 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)u64 *sptep =NULL ;uint retry_count =0 ; +/* +* TDX private mapping doesn't support fast page fault because the EPT +* entry needs TDX SEAMCALL. not direct memory access. +*/ +if (kvm_is_private_gpa (vcpu->kvm, fault->addr)) +return ret; +if (!page_fault_can_be_fast (fault))return ret;
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 039/104] KVM: x86/mmu: Disallow fast page fault on private GPA
Quoted 16 lines · On 3/4/22 20:48, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX requires TDX SEAMCALL to operate Secure EPT instead of direct memory access and TDX SEAMCALL is heavy operation. Fast page fault on private GPA doesn't make sense. Disallow fast page fault on private GPA.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/mmu.c | 7 +++++++ 1 file changed, 7 insertions(+)arch/x86/kvm/mmu/mmu.c +0
-0
arch/x86/kvm/mmu/mmu.c static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 3190
Quoted 6 lines
arch/x86/kvm/mmu/mmu.c +3
-0@@ -3185,6 +3185,13 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)u64 *sptep =NULL ;uint retry_count =0 ; +/* +* TDX private mapping doesn't support fast page fault because the EPT +* entry needs TDX SEAMCALL. not direct memory access.
"the EPT entry is read/written with TDX SEAMCALLs instead of direct memory access".
Quoted 7 lines
arch/x86/kvm/mmu/mmu.c +4
-0+*/ +if (kvm_is_private_gpa (vcpu->kvm, fault->addr)) +return ret; +if (!page_fault_can_be_fast (fault))return ret;
RFC PATCH v5 40/104 KVM: VMX: Split out guts of EPT violation to common/exposed function
In reply to [email protected]From: Sean Christopherson <[email protected]>
The difference of TDX EPT violation is how to retrieve information, GPA, and exit qualification. To share the code to handle EPT violation, split out the guts of EPT violation handler so that VMX/TDX exit handler can call it after retrieving GPA and exit qualification.
--- arch/x86/kvm/vmx/common.h | 35 +++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 34 ++++++---------------------------- 2 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 arch/x86/kvm/vmx/common.h
arch/x86/kvm/vmx/common.h new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_X86_VMX_COMMON_H +#define __KVM_X86_VMX_COMMON_H + +#include <linux/kvm_host.h> + +#include "mmu.h" + +static inline int __vmx_handle_ept_violation (struct kvm_vcpu *vcpu,gpa_t gpa, +unsigned long exit_qualification) +{ +u64 error_code; + +/* Is it a read fault? */ + error_code = (exit_qualification &EPT_VIOLATION_ACC_READ ) + ?PFERR_USER_MASK :0 ; +/* Is it a write fault? */ + error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) + ?PFERR_WRITE_MASK :0 ; +/* Is it a fetch fault? */ + error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) + ?PFERR_FETCH_MASK :0 ; +/* ept page table entry is present? */ + error_code |= (exit_qualification & + (EPT_VIOLATION_READABLE |EPT_VIOLATION_WRITABLE | +EPT_VIOLATION_EXECUTABLE )) + ?PFERR_PRESENT_MASK :0 ; + + error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) !=0 ? +PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; + +return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); +} + +#endif /* __KVM_X86_VMX_COMMON_H */
arch/x86/kvm/vmx/vmx.c +6 -28
@@ -50,6 +50,7 @@#include <asm/vmx.h> #include "capabilities.h" +#include "common.h" #include "cpuid.h" #include "evmcs.h" #include "hyperv.h"
@@ -5381,11 +5382,10 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)static int handle_ept_violation (struct kvm_vcpu *vcpu) { -unsigned long exit_qualification; -gpa_t gpa; -u64 error_code; +unsigned long exit_qualification =vmx_get_exit_qual (vcpu); +gpa_t gpa =vmcs_read64 (GUEST_PHYSICAL_ADDRESS ); - exit_qualification =vmx_get_exit_qual (vcpu); +trace_kvm_page_fault (gpa, exit_qualification);/* * EPT violation happened while executing iret from NMI,
@@ -5394,31 +5394,9 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)* AAK134, BY25. */ if (!(to_vmx (vcpu)->idt_vectoring_info &VECTORING_INFO_VALID_MASK ) && - enable_vnmi && - (exit_qualification &INTR_INFO_UNBLOCK_NMI )) + enable_vnmi && (exit_qualification &INTR_INFO_UNBLOCK_NMI ))vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO ,GUEST_INTR_STATE_NMI ); - gpa =vmcs_read64 (GUEST_PHYSICAL_ADDRESS ); -trace_kvm_page_fault (gpa, exit_qualification); - -/* Is it a read fault? */ - error_code = (exit_qualification &EPT_VIOLATION_ACC_READ ) - ?PFERR_USER_MASK :0 ; -/* Is it a write fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) - ?PFERR_WRITE_MASK :0 ; -/* Is it a fetch fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) - ?PFERR_FETCH_MASK :0 ; -/* ept page table entry is present? */ - error_code |= (exit_qualification & - (EPT_VIOLATION_READABLE |EPT_VIOLATION_WRITABLE | -EPT_VIOLATION_EXECUTABLE )) - ?PFERR_PRESENT_MASK :0 ; - - error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) !=0 ? -PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; - vcpu->arch.exit_qualification = exit_qualification;/*
@@ -5432,7 +5410,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)if (unlikely (allow_smaller_maxphyaddr &&kvm_vcpu_is_illegal_gpa (vcpu, gpa)))return kvm_emulate_instruction (vcpu,0 ); -return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); +return __vmx_handle_ept_violation (vcpu, gpa, exit_qualification); }static int handle_ept_misconfig (struct kvm_vcpu *vcpu)
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 040/104] KVM: VMX: Split out guts of EPT violation to common/exposed function
Quoted 117 lines · On 3/4/22 20:48, [email protected] wrote:
From: Sean Christopherson <[email protected]>
The difference of TDX EPT violation is how to retrieve information, GPA, and exit qualification. To share the code to handle EPT violation, split out the guts of EPT violation handler so that VMX/TDX exit handler can call it after retrieving GPA and exit qualification.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/common.h | 35 +++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 34 ++++++---------------------------- 2 files changed, 41 insertions(+), 28 deletions(-)create mode 100644
arch/x86/kvm/vmx/common.harch/x86/kvm/vmx/common.h new file +35
-0@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_X86_VMX_COMMON_H +#define __KVM_X86_VMX_COMMON_H + +#include <linux/kvm_host.h> + +#include "mmu.h" + +static inline int __vmx_handle_ept_violation (struct kvm_vcpu *vcpu,gpa_t gpa, +unsigned long exit_qualification) +{ +u64 error_code; + +/* Is it a read fault? */ + error_code = (exit_qualification &EPT_VIOLATION_ACC_READ ) + ?PFERR_USER_MASK :0 ; +/* Is it a write fault? */ + error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) + ?PFERR_WRITE_MASK :0 ; +/* Is it a fetch fault? */ + error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) + ?PFERR_FETCH_MASK :0 ; +/* ept page table entry is present? */ + error_code |= (exit_qualification & + (EPT_VIOLATION_READABLE |EPT_VIOLATION_WRITABLE | +EPT_VIOLATION_EXECUTABLE )) + ?PFERR_PRESENT_MASK :0 ; + + error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) !=0 ? +PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; + +return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); +} + +#endif /* __KVM_X86_VMX_COMMON_H */ arch/x86/kvm/vmx/vmx.c +5
-27@@ -50,6 +50,7 @@#include <asm/vmx.h> #include "capabilities.h" +#include "common.h" #include "cpuid.h" #include "evmcs.h" #include "hyperv.h" @@ -5381,11 +5382,10 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)static int handle_ept_violation (struct kvm_vcpu *vcpu) { -unsigned long exit_qualification; -gpa_t gpa; -u64 error_code; +unsigned long exit_qualification =vmx_get_exit_qual (vcpu); +gpa_t gpa =vmcs_read64 (GUEST_PHYSICAL_ADDRESS ); - exit_qualification =vmx_get_exit_qual (vcpu); +trace_kvm_page_fault (gpa, exit_qualification);/* * EPT violation happened while executing iret from NMI, @@ -5394,31 +5394,9 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)* AAK134, BY25. */ if (!(to_vmx (vcpu)->idt_vectoring_info &VECTORING_INFO_VALID_MASK ) && - enable_vnmi && - (exit_qualification &INTR_INFO_UNBLOCK_NMI )) + enable_vnmi && (exit_qualification &INTR_INFO_UNBLOCK_NMI ))vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO ,GUEST_INTR_STATE_NMI ); - gpa =vmcs_read64 (GUEST_PHYSICAL_ADDRESS ); -trace_kvm_page_fault (gpa, exit_qualification); - -/* Is it a read fault? */ - error_code = (exit_qualification &EPT_VIOLATION_ACC_READ ) - ?PFERR_USER_MASK :0 ; -/* Is it a write fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) - ?PFERR_WRITE_MASK :0 ; -/* Is it a fetch fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) - ?PFERR_FETCH_MASK :0 ; -/* ept page table entry is present? */ - error_code |= (exit_qualification & - (EPT_VIOLATION_READABLE |EPT_VIOLATION_WRITABLE | -EPT_VIOLATION_EXECUTABLE )) - ?PFERR_PRESENT_MASK :0 ; - - error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) !=0 ? -PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; - vcpu->arch.exit_qualification = exit_qualification;/*
arch/x86/kvm/vmx/vmx.c static int handle_ept_violation(struct kvm_vcpu *vcpu) · line 5416
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1
-1@@ -5432,7 +5410,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)if (unlikely (allow_smaller_maxphyaddr &&kvm_vcpu_is_illegal_gpa (vcpu, gpa)))return kvm_emulate_instruction (vcpu,0 ); -return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); +return __vmx_handle_ept_violation (vcpu, gpa, exit_qualification); }static int handle_ept_misconfig (struct kvm_vcpu *vcpu)
RFC PATCH v5 41/104 KVM: VMX: Move setting of EPT MMU masks to common VT-x code
In reply to [email protected]From: Sean Christopherson <[email protected]>
EPT MMU masks are used commonly for VMX and TDX. The value needs to be initialized in common code before both VMX/TDX-specific initialization code.
--- arch/x86/kvm/vmx/main.c | 5 +++++ arch/x86/kvm/vmx/vmx.c | 4 ---- 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/main.c +5 -0
@@ -4,6 +4,7 @@#include "x86_ops.h" #include "vmx.h" #include "nested.h" +#include "mmu.h" #include "pmu.h" #include "tdx.h"
@@ -22,6 +23,10 @@ static __init int vt_hardware_setup(void)tdx_hardware_setup (&vt_x86_ops); +if (enable_ept) +kvm_mmu_set_ept_masks (enable_ept_ad_bits, +cpu_has_vmx_ept_execute_only ()); +return 0 ; }
arch/x86/kvm/vmx/vmx.c +0 -4
@@ -7817,10 +7817,6 @@ __init int vmx_hardware_setup(void)set_bit (0 , vmx_vpid_bitmap);/* 0 is reserved for host */ -if (enable_ept) -kvm_mmu_set_ept_masks (enable_ept_ad_bits, -cpu_has_vmx_ept_execute_only ()); -kvm_configure_mmu (enable_ept,0 ,vmx_get_max_tdp_level (),ept_caps_to_lpage_level (vmx_capability.ept));
Signature · 1 line
2.25.1
Paolo Bonzini
maintainerRe: [RFC PATCH v5 041/104] KVM: VMX: Move setting of EPT MMU masks to common VT-x code
Quoted 41 lines · On 3/4/22 20:48, [email protected] wrote:
From: Sean Christopherson <[email protected]>
EPT MMU masks are used commonly for VMX and TDX. The value needs to be initialized in common code before both VMX/TDX-specific initialization code.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 5 +++++ arch/x86/kvm/vmx/vmx.c | 4 ---- 2 files changed, 5 insertions(+), 4 deletions(-)arch/x86/kvm/vmx/main.c +5
-0@@ -4,6 +4,7 @@#include "x86_ops.h" #include "vmx.h" #include "nested.h" +#include "mmu.h" #include "pmu.h" #include "tdx.h" @@ -22,6 +23,10 @@ static __init int vt_hardware_setup(void)tdx_hardware_setup (&vt_x86_ops); +if (enable_ept) +kvm_mmu_set_ept_masks (enable_ept_ad_bits, +cpu_has_vmx_ept_execute_only ()); +return 0 ; }arch/x86/kvm/vmx/vmx.c +0
-0
arch/x86/kvm/vmx/vmx.c __init int vmx_hardware_setup(void) · line 7822
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +0
-4@@ -7817,10 +7817,6 @@ __init int vmx_hardware_setup(void)set_bit (0 , vmx_vpid_bitmap);/* 0 is reserved for host */ -if (enable_ept) -kvm_mmu_set_ept_masks (enable_ept_ad_bits, -cpu_has_vmx_ept_execute_only ()); -kvm_configure_mmu (enable_ept,0 ,vmx_get_max_tdp_level (),ept_caps_to_lpage_level (vmx_capability.ept));
RB
RFC PATCH v5 42/104 KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
In reply to [email protected]From: Sean Christopherson <[email protected]>
Define the EPT Violation #VE control bit, #VE info VMCS fields, and the suppress #VE bit for EPT entries.
TDX will use a different shadow PTE entry value for MMIO from VMX. Add members to kvm_arch and track value for MMIO per-VM. By using per-VM EPT entry value for MMIO, the existing VMX logic is kept working.
In the case of VMX VM case, the EPT entry for MMIO is non-present PTE (present bit cleared) without backing guest physical address (on EPT violation, KVM seaching backing guest memory and it finds there is no backing guest page.) or the value to trigger EPT misconfiguration. For fast path. Once MMIO is triggered on the EPT entry, the EPT entry is updated for the future MMIO. It allows KVM to understand the memory access is for MMIO without searching backing guest pages.). And then KVM parses guest instruction to figure out address/value/width for MMIO.
In the case of the guest TD, the guest memory is protected so that VMM can't parse guest instruction to trigger EPT violation. Instead VMM sets up (Shared) EPT to trigger #VE. When the guest TD issues MMIO, #VE is injected. guest VE handler converts MMIO access into MMIO hypercall to pass address/value/width for MMIO to VMM. (or directly paravirtualize MMIO into hypercall.) Then VMM can handle the MMIO hypercall without parsing guest instruction.
When the guest accesses GPA if "the EPT Violation #VE" control bit is set and EPT SUPPRESS VE bit in EPT entry is cleared, #VE, virtualization exception, is injected into the guest. Because the TDX guest vCPU state and memory are protected, a VMM can't emulate MMIO by the TDX guest on EPT violation by snooping vCPU state and parsing instruction to figure out MMIO address and value. Instead, PV MMIO (MMIO hypercall) is adapted. On EPT violation, CPU injects #VE to guest and the guest converts MMIO instruction into PV MMIO. Or guest directly issues MMIO hypercall.
The existing VMX code uses zero as an initial value for EPT entry. TDX will enable EPT-violation #VE VM-execution control and requires suppress VE bit cleared in shared EPT entry to inject #VE into the TDX guest. To keep the same behavior for VMX, suppress VE bit needs to be set. Allow to specify an initial value for EPT entry and if TDX is enabled, set initial EPT entry value to suppress VE bit set. EPT-violation #VE VM-execution control will be enabled, and For TDX shared EPT suppress VE bit will be cleared for TDX shared EPT entry.
--- arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/mmu.h | 6 ++++-- arch/x86/kvm/mmu/mmu.c | 19 +++++++++++------ arch/x86/kvm/mmu/spte.c | 38 ++++++++++++++++----------------- arch/x86/kvm/mmu/spte.h | 9 ++++---- arch/x86/kvm/mmu/tdp_mmu.c | 6 +++--- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/main.c | 7 ++++-- arch/x86/kvm/vmx/tdx.c | 2 +- arch/x86/kvm/vmx/tdx.h | 2 ++ arch/x86/kvm/vmx/vmx.c | 8 +++++++ 12 files changed, 63 insertions(+), 40 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
@@ -1069,6 +1069,9 @@ struct kvm_arch {*/ spinlock_t mmu_unsync_pages_lock; +u64 shadow_mmio_value; +u64 shadow_mmio_mask; +struct list_head assigned_dev_head;struct iommu_domain *iommu_domain;bool iommu_noncoherent;
arch/x86/include/asm/vmx.h +1 -0
@@ -498,6 +498,7 @@ enum vmcs_field {#define VMX_EPT_IPAT_BIT (1ull << 6)#define VMX_EPT_ACCESS_BIT (1ull << 8)#define VMX_EPT_DIRTY_BIT (1ull << 9) +#define VMX_EPT_SUPPRESS_VE_BIT (1ull << 63)#define VMX_EPT_RWX_MASK (VMX_EPT_READABLE_MASK | \ VMX_EPT_WRITABLE_MASK | \ VMX_EPT_EXECUTABLE_MASK)
arch/x86/kvm/mmu.h +4 -2
@@ -64,8 +64,10 @@ static __always_inline u64 rsvd_bits(int s, int e)return ((2ULL << (e - s)) -1 ) << s; } -void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask); -void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only); +void kvm_mmu_set_mmio_spte_mask (struct kvm *kvm,u64 mmio_value,u64 mmio_mask, +u64 access_mask); +void kvm_mmu_set_default_mmio_spte_mask (u64 mask); +void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only,u64 init_value);void kvm_mmu_set_spte_init_value (u64 init_value);void kvm_init_mmu (struct kvm_vcpu *vcpu);
arch/x86/kvm/mmu/mmu.c +13 -6
@@ -2336,7 +2336,7 @@ static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,return kvm_mmu_prepare_zap_page (kvm, child, invalid_list); } - }else if (is_mmio_spte (pte)) { + }else if (is_mmio_spte (kvm, pte)) {mmu_spte_clear_no_track (spte); }return 0 ;
@@ -3069,9 +3069,12 @@ static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fa/* * If MMIO caching is disabled, emulate immediately without * touching the shadow page tables as attempting to install an -* MMIO SPTE will just be an expensive nop. +* MMIO SPTE will just be an expensive nop, but excludes the +* INTEL TD guest due to it also uses shadow_mmio_value = 0 +* to emulating MMIO access. */ -if (unlikely (!shadow_mmio_value)) { +if (unlikely (!vcpu->kvm->arch.shadow_mmio_value) + && !kvm_gfn_stolen_mask (vcpu->kvm)) { *ret_val =RET_PF_EMULATE ;return true; }
@@ -3209,7 +3212,8 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)break ; sp =sptep_to_sp (sptep); -if (!is_last_spte (spte, sp->role.level) ||is_mmio_spte (spte)) +if (!is_last_spte (spte, sp->role.level) || +is_mmio_spte (vcpu->kvm, spte))break ;/*
@@ -3892,7 +3896,7 @@ static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)if (WARN_ON (reserved))return -EINVAL ; -if (is_mmio_spte (spte)) { +if (is_mmio_spte (vcpu->kvm, spte)) {gfn_t gfn =get_mmio_spte_gfn (spte);unsigned int access =get_mmio_spte_access (spte);
@@ -4294,7 +4298,7 @@ static unsigned long get_cr3(struct kvm_vcpu *vcpu)static bool sync_mmio_spte (struct kvm_vcpu *vcpu,u64 *sptep,gfn_t gfn,unsigned int access) { -if (unlikely (is_mmio_spte (*sptep))) { +if (unlikely (is_mmio_spte (vcpu->kvm, *sptep))) {if (gfn !=get_mmio_spte_gfn (*sptep)) {mmu_spte_clear_no_track (sptep);return true;
@@ -5791,6 +5795,9 @@ void kvm_mmu_init_vm(struct kvm *kvm)kvm_page_track_register_notifier (kvm, node); kvm->arch.tdp_max_page_level =KVM_MAX_HUGEPAGE_LEVEL ; +kvm_mmu_set_mmio_spte_mask (kvm, shadow_default_mmio_mask, + shadow_default_mmio_mask, +ACC_WRITE_MASK |ACC_USER_MASK ); }void kvm_mmu_uninit_vm (struct kvm *kvm)
arch/x86/kvm/mmu/spte.c +18 -20
@@ -29,8 +29,7 @@ u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */u64 __read_mostly shadow_user_mask;u64 __read_mostly shadow_accessed_mask;u64 __read_mostly shadow_dirty_mask; -u64 __read_mostly shadow_mmio_value; -u64 __read_mostly shadow_mmio_mask; +u64 __read_mostly shadow_default_mmio_mask;u64 __read_mostly shadow_mmio_access_mask;u64 __read_mostly shadow_present_mask;u64 __read_mostly shadow_me_mask;
@@ -59,10 +58,11 @@ u64 make_mmio_spte(struct kvm_vcpu *vcpu, u64 gfn, unsigned int access)u64 spte =generation_mmio_spte_mask (gen);u64 gpa = gfn <<PAGE_SHIFT ; -WARN_ON_ONCE (!shadow_mmio_value); +WARN_ON_ONCE (!vcpu->kvm->arch.shadow_mmio_value && + !kvm_gfn_stolen_mask (vcpu->kvm)); access &= shadow_mmio_access_mask; - spte |= shadow_mmio_value | access; + spte |= vcpu->kvm->arch.shadow_mmio_value | access; spte |= gpa | shadow_nonpresent_or_rsvd_mask; spte |= (gpa & shadow_nonpresent_or_rsvd_mask) <<SHADOW_NONPRESENT_OR_RSVD_MASK_LEN ;
@@ -279,7 +279,8 @@ u64 mark_spte_for_access_track(u64 spte)return spte; } -void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask) +void kvm_mmu_set_mmio_spte_mask (struct kvm *kvm,u64 mmio_value,u64 mmio_mask, +u64 access_mask) {BUG_ON ((u64 )(unsigned )access_mask != access_mask);WARN_ON (mmio_value & shadow_nonpresent_or_rsvd_lower_gfn_mask);
@@ -308,39 +309,32 @@ void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask) WARN_ON(mmio_value && (REMOVED_SPTE & mmio_mask) == mmio_value)) mmio_value =0 ; - shadow_mmio_value = mmio_value; - shadow_mmio_mask = mmio_mask; + kvm->arch.shadow_mmio_value = mmio_value; + kvm->arch.shadow_mmio_mask = mmio_mask; shadow_mmio_access_mask = access_mask; }EXPORT_SYMBOL_GPL (kvm_mmu_set_mmio_spte_mask); -void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only) +void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only,u64 init_value) { shadow_user_mask =VMX_EPT_READABLE_MASK ; shadow_accessed_mask = has_ad_bits ?VMX_EPT_ACCESS_BIT :0ull ; shadow_dirty_mask = has_ad_bits ?VMX_EPT_DIRTY_BIT :0ull ; shadow_nx_mask =0ull ; shadow_x_mask =VMX_EPT_EXECUTABLE_MASK ; - shadow_present_mask = has_exec_only ?0ull :VMX_EPT_READABLE_MASK ; + shadow_present_mask = + (has_exec_only ?0ull :VMX_EPT_READABLE_MASK ) | init_value; shadow_acc_track_mask =VMX_EPT_RWX_MASK ; shadow_me_mask =0ull ; shadow_host_writable_mask =EPT_SPTE_HOST_WRITABLE ; shadow_mmu_writable_mask =EPT_SPTE_MMU_WRITABLE ; - -/* -* EPT Misconfigurations are generated if the value of bits 2:0 -* of an EPT paging-structure entry is 110b (write/execute). -*/ -kvm_mmu_set_mmio_spte_mask (VMX_EPT_MISCONFIG_WX_VALUE , -VMX_EPT_RWX_MASK ,0 ); }EXPORT_SYMBOL_GPL (kvm_mmu_set_ept_masks);void kvm_mmu_reset_all_pte_masks (void ) {u8 low_phys_bits; -u64 mask; shadow_phys_bits =kvm_get_shadow_phys_bits ();
@@ -389,9 +383,13 @@ void kvm_mmu_reset_all_pte_masks(void)* PTEs and so the reserved PA approach must be disabled. */ if (shadow_phys_bits <52 ) - mask =BIT_ULL (51 ) |PT_PRESENT_MASK ; + shadow_default_mmio_mask =BIT_ULL (51 ) |PT_PRESENT_MASK ;else - mask =0 ; + shadow_default_mmio_mask =0 ; +} -kvm_mmu_set_mmio_spte_mask (mask, mask,ACC_WRITE_MASK |ACC_USER_MASK ); +void kvm_mmu_set_default_mmio_spte_mask (u64 mask) +{ + shadow_default_mmio_mask = mask; } +EXPORT_SYMBOL_GPL (kvm_mmu_set_default_mmio_spte_mask);
arch/x86/kvm/mmu/spte.h +4 -5
@@ -165,8 +165,7 @@ extern u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */extern u64 __read_mostly shadow_user_mask;extern u64 __read_mostly shadow_accessed_mask;extern u64 __read_mostly shadow_dirty_mask; -extern u64 __read_mostly shadow_mmio_value; -extern u64 __read_mostly shadow_mmio_mask; +extern u64 __read_mostly shadow_default_mmio_mask;extern u64 __read_mostly shadow_mmio_access_mask;extern u64 __read_mostly shadow_present_mask;extern u64 __read_mostly shadow_me_mask;
@@ -229,10 +228,10 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask;*/ extern u8 __read_mostly shadow_phys_bits; -static inline bool is_mmio_spte (u64 spte) +static inline bool is_mmio_spte (struct kvm *kvm,u64 spte) { -return (spte & shadow_mmio_mask) == shadow_mmio_value && -likely (shadow_mmio_value); +return (spte & kvm->arch.shadow_mmio_mask) == kvm->arch.shadow_mmio_value && +likely (kvm->arch.shadow_mmio_value ||kvm_gfn_stolen_mask (kvm)); }static inline bool is_shadow_present_pte (u64 pte)
arch/x86/kvm/mmu/tdp_mmu.c +3 -3
@@ -447,8 +447,8 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* impact the guest since both the former and current SPTEs * are nonpresent. */ -if (WARN_ON (!is_mmio_spte (old_spte) && - !is_mmio_spte (new_spte) && +if (WARN_ON (!is_mmio_spte (kvm, old_spte) && + !is_mmio_spte (kvm, new_spte) && !is_removed_spte (new_spte)))pr_err ("Unexpected SPTE change! Nonpresent SPTEs\n" "should not be replaced with another,\n"
@@ -927,7 +927,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, }/* If a MMIO SPTE is installed, the MMIO will need to be emulated. */ -if (unlikely (is_mmio_spte (new_spte))) { +if (unlikely (is_mmio_spte (vcpu->kvm, new_spte))) {trace_mark_mmio_spte (rcu_dereference (iter->sptep), iter->gfn, new_spte); ret =RET_PF_EMULATE ;
arch/x86/kvm/svm/svm.c +1 -1
@@ -4704,7 +4704,7 @@ static __init void svm_adjust_mmio_mask(void)*/ mask = (mask_bit <52 ) ?rsvd_bits (mask_bit,51 ) |PT_PRESENT_MASK :0 ; -kvm_mmu_set_mmio_spte_mask (mask, mask,PT_WRITABLE_MASK |PT_USER_MASK ); +kvm_mmu_set_default_mmio_spte_mask (mask); }static __init void svm_set_cpu_caps (void )
arch/x86/kvm/vmx/main.c +5 -2
@@ -23,9 +23,12 @@ static __init int vt_hardware_setup(void)tdx_hardware_setup (&vt_x86_ops); -if (enable_ept) +if (enable_ept) { +const u64 init_value = enable_tdx ?VMX_EPT_SUPPRESS_VE_BIT :0ull ;kvm_mmu_set_ept_masks (enable_ept_ad_bits, -cpu_has_vmx_ept_execute_only ()); +cpu_has_vmx_ept_execute_only (), init_value); +kvm_mmu_set_spte_init_value (init_value); + }return 0 ; }
arch/x86/kvm/vmx/tdx.c +1 -1
@@ -11,7 +11,7 @@#undef pr_fmt#define pr_fmt (fmt) "tdx: " fmt -static bool __read_mostly enable_tdx = true; +bool __read_mostly enable_tdx = true;module_param_named (tdx, enable_tdx, bool,0644 );#define TDX_MAX_NR_CPUID_CONFIGS \
arch/x86/kvm/vmx/tdx.h +2 -0
@@ -6,6 +6,7 @@#include "tdx_ops.h" +extern bool __read_mostly enable_tdx;int tdx_module_setup (void );struct tdx_td_page {
@@ -166,6 +167,7 @@ static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 fiel }#else +#define enable_tdx falsestatic inline int tdx_module_setup (void ) {return -ENODEV ; };struct kvm_tdx ;
arch/x86/kvm/vmx/vmx.c +8 -0
@@ -7065,6 +7065,14 @@ int vmx_vm_init(struct kvm *kvm)if (!ple_gap) kvm->arch.pause_in_guest = true; +/* +* EPT Misconfigurations can be generated if the value of bits 2:0 +* of an EPT paging-structure entry is 110b (write/execute). +*/ +if (enable_ept) +kvm_mmu_set_mmio_spte_mask (kvm,VMX_EPT_MISCONFIG_WX_VALUE , +VMX_EPT_MISCONFIG_WX_VALUE ,0 ); +if (boot_cpu_has (X86_BUG_L1TF ) && enable_ept) {switch (l1tf_mitigation) {case L1TF_MITIGATION_OFF :
Signature · 1 line
2.25.1
Paolo Bonzini
maintainerRe: [RFC PATCH v5 042/104] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 31
Quoted 7 lines · On 3/4/22 20:48, [email protected] wrote:
arch/x86/kvm/vmx/main.c +5
-1+if (enable_ept) { +const u64 init_value = enable_tdx ?VMX_EPT_SUPPRESS_VE_BIT :0ull ;kvm_mmu_set_ept_masks (enable_ept_ad_bits, -cpu_has_vmx_ept_execute_only ()); +cpu_has_vmx_ept_execute_only (), init_value); +kvm_mmu_set_spte_init_value (init_value); + }
I think kvm-intel.ko should use VMX_EPT_SUPPRESS_VE_BIT unconditionally as the init value. The bit is ignored anyway if the "EPT-violation #VE" execution control is 0. Otherwise looks good, but I have a couple more crazy ideas:
1) there could even be a test mode where KVM enables the execution control, traps #VE in the exception bitmap, and shouts loudly if it gets a #VE. That might avoid hard-to-find bugs due to forgetting about VMX_EPT_SUPPRESS_VE_BIT.
2) or even, perhaps the init_value for the TDP MMU could set bit 63 _unconditionally_, because KVM always sets the NX bit on AMD hardware. That would remove the whole infrastructure to keep shadow_init_value, because it would be constant 0 in mmu.c and constant BIT(63) in tdp_mmu.c.
Sean, what do you think?
Paolo
Re: [RFC PATCH v5 042/104] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
Quoted 19 lines · Paolo Bonzini
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 31
Quoted 7 lines · On 3/4/22 20:48, [email protected] wrote:
arch/x86/kvm/vmx/main.c +5
-1+if (enable_ept) { +const u64 init_value = enable_tdx ?VMX_EPT_SUPPRESS_VE_BIT :0ull ;kvm_mmu_set_ept_masks (enable_ept_ad_bits, -cpu_has_vmx_ept_execute_only ()); +cpu_has_vmx_ept_execute_only (), init_value); +kvm_mmu_set_spte_init_value (init_value); + }I think kvm-intel.ko should use
VMX_EPT_SUPPRESS_VE_BITunconditionally as the init value. The bit is ignored anyway if the "EPT-violation #VE" execution control is 0. Otherwise looks good, but I have a couple more crazy ideas:1) there could even be a test mode where KVM enables the execution control, traps #VE in the exception bitmap, and shouts loudly if it gets a #VE. That might avoid hard-to-find bugs due to forgetting about
VMX_EPT_SUPPRESS_VE_BIT.
Quoted 6 lines
2) or even, perhaps the
init_valuefor the TDP MMU could set bit 63 _unconditionally_, because KVM always sets the NX bit on AMD hardware. That would remove the whole infrastructure to keepshadow_init_value, because it would be constant 0 inmmu.cand constant BIT(63) intdp_mmu.c.Sean, what do you think?
Then, I'll start with 1) because it's a bit hard for me to test 2) with real AMD hardware. If someone is willing to test 2), I'm quite fine to implement 2) on top of 1). 2) isn't exclusive with 1).
Signature · 1 line
Isaku Yamahata <[email protected]>
Sean Christopherson
maintainerRe: [RFC PATCH v5 042/104] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
Sorry, missed my name...
Quoted 17 lines · Isaku Yamahata
Quoted 14 lines · Paolo Bonzini
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 31
Quoted 7 lines · On 3/4/22 20:48, [email protected] wrote:
arch/x86/kvm/vmx/main.c +5
-1+if (enable_ept) { +const u64 init_value = enable_tdx ?VMX_EPT_SUPPRESS_VE_BIT :0ull ;kvm_mmu_set_ept_masks (enable_ept_ad_bits, -cpu_has_vmx_ept_execute_only ()); +cpu_has_vmx_ept_execute_only (), init_value); +kvm_mmu_set_spte_init_value (init_value); + }I think kvm-intel.ko should use
VMX_EPT_SUPPRESS_VE_BITunconditionally as the init value. The bit is ignored anyway if the "EPT-violation #VE" execution control is 0.
Otherwise looks good, but I have a couple more crazy ideas:
Quoted 7 lines
Quoted 7 lines
1) there could even be a test mode where KVM enables the execution control, traps #VE in the exception bitmap, and shouts loudly if it gets a #VE. That might avoid hard-to-find bugs due to forgetting about
VMX_EPT_SUPPRESS_VE_BIT.2) or even, perhaps the
init_valuefor the TDP MMU could set bit 63 _unconditionally_, because KVM always sets the NX bit on AMD hardware.
Heh, took me a minute to realize you mean EFER.NX. To clarify:
KVM requires NX support in hardware
if (!boot_cpu_has(X86_FEATURE_NX)) {
pr_err_ratelimited("NX (Execute Disable) not supported\n");
return -EOPNOTSUPP;
}and 64-bit or PAE paging to enable NPT
if (!IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_X86_PAE)) npt_enabled = false;
and the _kernel_ forces EFER.NX=1 for 64-bit and PAE kernels.
But whether or not EFER.NX is enabled is irrelevant, it's only the initial value, i.e. the SPTE is guaranteed to be !PRESENT, so hardware will never generate a reserved bit #PF.
Quoted 4 lines
Quoted 4 lines
That would remove the whole infrastructure to keep
shadow_init_value, because it would be constant 0 inmmu.cand constant BIT(63) intdp_mmu.c.Sean, what do you think?
I like #2, though I suspect we'll still want shadow_init_value so that the MMU
caches can be shared without creating a mess. But I still like keeping that
detail in the MMUs and out of the vendor modules, even though there's obviously a hard dependency on the MMU doing the right thing.
Quoted 3 lines
Then, I'll start with 1) because it's a bit hard for me to test 2) with real AMD hardware. If someone is willing to test 2), I'm quite fine to implement 2) on top of 1). 2) isn't exclusive with 1).
I can test #2.
Tangentially related, the kvm_gfn_stolen_mask() exception to MMIO SPTEs is unnecessarily convoluted and gross. That's partly my fault as I should have just updated enable_mmio_caching when hardware can't support it instead of using shadow_mmio_value to convey that information. I'll submit a patch to fix that, then is_mmio_spte() can be left alone in the TDX series.
Re: [RFC PATCH v5 042/104] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
In reply to [email protected]Quoted 4 lines · On Fri, 2022-03-04 at 11:48 -0800, [email protected] wrote:
From: Sean Christopherson <[email protected]>
Define the EPT Violation #VE control bit, #VE info VMCS fields, and the suppress #VE bit for EPT entries.
It appears only the last one is introduced in this patch.
Quoted 5 lines
TDX will use a different shadow PTE entry value for MMIO from VMX. Add members to
kvm_archand track value for MMIO per-VM. By using per-VM EPT entry value for MMIO, the existing VMX logic is kept working.
Quoted 8 lines
In the case of VMX VM case, the EPT entry for MMIO is non-present PTE (present bit cleared) without backing guest physical address (on EPT violation, KVM seaching backing guest memory and it finds there is no backing guest page.) or the value to trigger EPT misconfiguration. For fast path. Once MMIO is triggered on the EPT entry, the EPT entry is updated for the future MMIO. It allows KVM to understand the memory access is for MMIO without searching backing guest pages.). And then KVM parses guest instruction to figure out address/value/width for MMIO.
What does "For fast path." meaning?
There are also grammar issues in above paragraph.
Quoted 4 lines
In the case of the guest TD, the guest memory is protected so that VMM can't parse guest instruction to trigger EPT violation.
Even VMM can parse guest instruction, it cannot trigger EPT violatoin. Only guest can trigger EPT violation.
Quoted 6 lines
Instead VMM sets
up (Shared) EPT to trigger #VE. When the guest TD issues MMIO, #VE is injected. guest VE handler converts MMIO access into MMIO hypercall to pass address/value/width for MMIO to VMM. (or directly paravirtualize MMIO into hypercall.) Then VMM can handle the MMIO hypercall without parsing guest instruction.
There are couple of grammar issues in above two paragraphs.
Quoted 9 lines
When the guest accesses GPA if "the EPT Violation #VE" control bit is set and EPT SUPPRESS VE bit in EPT entry is cleared, #VE, virtualization exception, is injected into the guest. Because the TDX guest vCPU state and memory are protected, a VMM can't emulate MMIO by the TDX guest on EPT violation by snooping vCPU state and parsing instruction to figure out MMIO address and value. Instead, PV MMIO (MMIO hypercall) is adapted. On EPT violation, CPU injects #VE to guest and the guest converts MMIO instruction into PV MMIO. Or guest directly issues MMIO hypercall.
Isn't this paragraph kinda duplicated with above paragraph?
Quoted 9 lines
The existing VMX code uses zero as an initial value for EPT entry. TDX will enable EPT-violation #VE VM-execution control and requires suppress VE bit cleared in shared EPT entry to inject #VE into the TDX guest. To keep the same behavior for VMX, suppress VE bit needs to be set. Allow to specify an initial value for EPT entry and if TDX is enabled, set initial EPT entry value to suppress VE bit set. EPT-violation #VE VM-execution control will be enabled, and For TDX shared EPT suppress VE bit will be cleared for TDX shared EPT entry.
Isn't the last paragraph talking about the same thing as you did in patch 37 "KVM: x86/mmu: Allow non-zero init value for shadow PTE"? That patch has already done the thing to set a non-zero initial PTE.
Btw, please put this patch and patch 37 together since they are handling similar thing (now there are couple of unrelated patches in the middle).
This patch talks about changing MMIO value/mask from global to per-VM tracking. Please focus on explaining the rational behind this -- i.e. unlike to legacy VMX VM (and AMD legacy VMs), TD guest requires different MMIO value/mask in order to configure MMIO EPT entry to not generate EPT misconfiguration, but instead to inject #VE by setting up a non-present SPTE which causes EPT violation but with "Suppress #VE" bit clear.
Quoted 75 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/mmu.h | 6 ++++-- arch/x86/kvm/mmu/mmu.c | 19 +++++++++++------ arch/x86/kvm/mmu/spte.c | 38 ++++++++++++++++----------------- arch/x86/kvm/mmu/spte.h | 9 ++++---- arch/x86/kvm/mmu/tdp_mmu.c | 6 +++--- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/main.c | 7 ++++-- arch/x86/kvm/vmx/tdx.c | 2 +- arch/x86/kvm/vmx/tdx.h | 2 ++ arch/x86/kvm/vmx/vmx.c | 8 +++++++ 12 files changed, 63 insertions(+), 40 deletions(-)arch/x86/include/asm/kvm_host.h +3
-0@@ -1069,6 +1069,9 @@ struct kvm_arch {*/ spinlock_t mmu_unsync_pages_lock; +u64 shadow_mmio_value; +u64 shadow_mmio_mask; +struct list_head assigned_dev_head;struct iommu_domain *iommu_domain;bool iommu_noncoherent;arch/x86/include/asm/vmx.h +1
-0@@ -498,6 +498,7 @@ enum vmcs_field {#define VMX_EPT_IPAT_BIT (1ull << 6)#define VMX_EPT_ACCESS_BIT (1ull << 8)#define VMX_EPT_DIRTY_BIT (1ull << 9) +#define VMX_EPT_SUPPRESS_VE_BIT (1ull << 63)#define VMX_EPT_RWX_MASK (VMX_EPT_READABLE_MASK | \ VMX_EPT_WRITABLE_MASK | \ VMX_EPT_EXECUTABLE_MASK)arch/x86/kvm/mmu.h +4
-2@@ -64,8 +64,10 @@ static __always_inline u64 rsvd_bits(int s, int e)return ((2ULL << (e - s)) -1 ) << s; } -void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask); -void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only); +void kvm_mmu_set_mmio_spte_mask (struct kvm *kvm,u64 mmio_value,u64 mmio_mask, +u64 access_mask); +void kvm_mmu_set_default_mmio_spte_mask (u64 mask); +void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only,u64 init_value);void kvm_mmu_set_spte_init_value (u64 init_value);void kvm_init_mmu (struct kvm_vcpu *vcpu);arch/x86/kvm/mmu/mmu.c +1
-1@@ -2336,7 +2336,7 @@ static int mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,return kvm_mmu_prepare_zap_page (kvm, child, invalid_list); } - }else if (is_mmio_spte (pte)) { + }else if (is_mmio_spte (kvm, pte)) {mmu_spte_clear_no_track (spte); }return 0 ;
arch/x86/kvm/mmu/mmu.c static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fa · line 3074
Quoted 7 lines
arch/x86/kvm/mmu/mmu.c +3
-1@@ -3069,9 +3069,12 @@ static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fa/* * If MMIO caching is disabled, emulate immediately without * touching the shadow page tables as attempting to install an -* MMIO SPTE will just be an expensive nop. +* MMIO SPTE will just be an expensive nop, but excludes the +* INTEL TD guest due to it also uses shadow_mmio_value = 0 +* to emulating MMIO access.
The comment seems is not accurate. If I read correctly, for a TD guest, shadow_mmio_value is initialized to shadow_default_mmio_mask, which isn't always
0. See changes to kvm_mmu_reset_all_pte_masks().
arch/x86/kvm/mmu/mmu.c static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fa · line 3077
Quoted 4 lines
arch/x86/kvm/mmu/mmu.c +2
-1*/ -if (unlikely (!shadow_mmio_value)) { +if (unlikely (!vcpu->kvm->arch.shadow_mmio_value) + && !kvm_gfn_stolen_mask (vcpu->kvm)) {
I don't like using kvm_gfn_stolen_mask() here. Similar to below comment related to is_mmio_spte().
Quoted 92 lines
arch/x86/kvm/mmu/mmu.c +7
-3*ret_val =RET_PF_EMULATE ;return true; }@@ -3209,7 +3212,8 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)break ; sp =sptep_to_sp (sptep); -if (!is_last_spte (spte, sp->role.level) ||is_mmio_spte (spte)) +if (!is_last_spte (spte, sp->role.level) || +is_mmio_spte (vcpu->kvm, spte))break ;/* @@ -3892,7 +3896,7 @@ static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)if (WARN_ON (reserved))return -EINVAL ; -if (is_mmio_spte (spte)) { +if (is_mmio_spte (vcpu->kvm, spte)) {gfn_t gfn =get_mmio_spte_gfn (spte);unsigned int access =get_mmio_spte_access (spte);@@ -4294,7 +4298,7 @@ static unsigned long get_cr3(struct kvm_vcpu *vcpu)static bool sync_mmio_spte (struct kvm_vcpu *vcpu,u64 *sptep,gfn_t gfn,unsigned int access) { -if (unlikely (is_mmio_spte (*sptep))) { +if (unlikely (is_mmio_spte (vcpu->kvm, *sptep))) {if (gfn !=get_mmio_spte_gfn (*sptep)) {mmu_spte_clear_no_track (sptep);return true;@@ -5791,6 +5795,9 @@ void kvm_mmu_init_vm(struct kvm *kvm)kvm_page_track_register_notifier (kvm, node); kvm->arch.tdp_max_page_level =KVM_MAX_HUGEPAGE_LEVEL ; +kvm_mmu_set_mmio_spte_mask (kvm, shadow_default_mmio_mask, + shadow_default_mmio_mask, +ACC_WRITE_MASK |ACC_USER_MASK ); }void kvm_mmu_uninit_vm (struct kvm *kvm)arch/x86/kvm/mmu/spte.c +8
-8@@ -29,8 +29,7 @@ u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */u64 __read_mostly shadow_user_mask;u64 __read_mostly shadow_accessed_mask;u64 __read_mostly shadow_dirty_mask; -u64 __read_mostly shadow_mmio_value; -u64 __read_mostly shadow_mmio_mask; +u64 __read_mostly shadow_default_mmio_mask;u64 __read_mostly shadow_mmio_access_mask;u64 __read_mostly shadow_present_mask;u64 __read_mostly shadow_me_mask;@@ -59,10 +58,11 @@ u64 make_mmio_spte(struct kvm_vcpu *vcpu, u64 gfn, unsigned int access)u64 spte =generation_mmio_spte_mask (gen);u64 gpa = gfn <<PAGE_SHIFT ; -WARN_ON_ONCE (!shadow_mmio_value); +WARN_ON_ONCE (!vcpu->kvm->arch.shadow_mmio_value && + !kvm_gfn_stolen_mask (vcpu->kvm)); access &= shadow_mmio_access_mask; - spte |= shadow_mmio_value | access; + spte |= vcpu->kvm->arch.shadow_mmio_value | access; spte |= gpa | shadow_nonpresent_or_rsvd_mask; spte |= (gpa & shadow_nonpresent_or_rsvd_mask) <<SHADOW_NONPRESENT_OR_RSVD_MASK_LEN ;@@ -279,7 +279,8 @@ u64 mark_spte_for_access_track(u64 spte)return spte; } -void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask) +void kvm_mmu_set_mmio_spte_mask (struct kvm *kvm,u64 mmio_value,u64 mmio_mask, +u64 access_mask) {BUG_ON ((u64 )(unsigned )access_mask != access_mask);WARN_ON (mmio_value & shadow_nonpresent_or_rsvd_lower_gfn_mask);@@ -308,39 +309,32 @@ void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask) WARN_ON(mmio_value && (REMOVED_SPTE & mmio_mask) == mmio_value)) mmio_value =0 ; - shadow_mmio_value = mmio_value; - shadow_mmio_mask = mmio_mask; + kvm->arch.shadow_mmio_value = mmio_value; + kvm->arch.shadow_mmio_mask = mmio_mask; shadow_mmio_access_mask = access_mask; }EXPORT_SYMBOL_GPL (kvm_mmu_set_mmio_spte_mask); -void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only)
arch/x86/kvm/mmu/spte.c void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask) · line 326
Quoted 10 lines
arch/x86/kvm/mmu/spte.c +3
-1+void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only,u64 init_value) { shadow_user_mask =VMX_EPT_READABLE_MASK ; shadow_accessed_mask = has_ad_bits ?VMX_EPT_ACCESS_BIT :0ull ; shadow_dirty_mask = has_ad_bits ?VMX_EPT_DIRTY_BIT :0ull ; shadow_nx_mask =0ull ; shadow_x_mask =VMX_EPT_EXECUTABLE_MASK ; - shadow_present_mask = has_exec_only ?0ull :VMX_EPT_READABLE_MASK ; + shadow_present_mask = + (has_exec_only ?0ull :VMX_EPT_READABLE_MASK ) | init_value;
This change doesn't seem make any sense. Why should "Suppress #VE" bit be set for a present PTE?
Quoted 23 lines
arch/x86/kvm/mmu/spte.c +0
-8shadow_acc_track_mask =VMX_EPT_RWX_MASK ; shadow_me_mask =0ull ; shadow_host_writable_mask =EPT_SPTE_HOST_WRITABLE ; shadow_mmu_writable_mask =EPT_SPTE_MMU_WRITABLE ; - -/* -* EPT Misconfigurations are generated if the value of bits 2:0 -* of an EPT paging-structure entry is 110b (write/execute). -*/ -kvm_mmu_set_mmio_spte_mask (VMX_EPT_MISCONFIG_WX_VALUE , -VMX_EPT_RWX_MASK ,0 ); }EXPORT_SYMBOL_GPL (kvm_mmu_set_ept_masks);void kvm_mmu_reset_all_pte_masks (void ) {u8 low_phys_bits; -u64 mask; shadow_phys_bits =kvm_get_shadow_phys_bits ();
arch/x86/kvm/mmu/spte.c void kvm_mmu_reset_all_pte_masks(void) · line 386
Quoted 5 lines
arch/x86/kvm/mmu/spte.c +1
-1@@ -389,9 +383,13 @@ void kvm_mmu_reset_all_pte_masks(void)* PTEs and so the reserved PA approach must be disabled. */ if (shadow_phys_bits <52 ) - mask =BIT_ULL (51 ) |PT_PRESENT_MASK ; + shadow_default_mmio_mask =BIT_ULL (51 ) |PT_PRESENT_MASK ;
Hmm... Not related to this patch, but it seems there's a bug here. On a MKTME enabled system (but not TDX) with 52 physical bits, the shadow_phys_bits will be set to < 52 (depending on how many MKTME KeyIDs are configured by BIOS). In this case, bit 51 is set, but actually bit 51 isn't a reserved bit in this case. Instead, it is a MKTME KeyID bit. Therefore, above setting won't cause #PF, but will use a non-zero MKTME keyID to access the physical address.
Paolo/Sean, any comments here?
Quoted 26 lines
arch/x86/kvm/mmu/spte.c +6
-2else - mask =0 ; + shadow_default_mmio_mask =0 ; +} -kvm_mmu_set_mmio_spte_mask (mask, mask,ACC_WRITE_MASK |ACC_USER_MASK ); +void kvm_mmu_set_default_mmio_spte_mask (u64 mask) +{ + shadow_default_mmio_mask = mask; } +EXPORT_SYMBOL_GPL (kvm_mmu_set_default_mmio_spte_mask);arch/x86/kvm/mmu/spte.h +1
-2@@ -165,8 +165,7 @@ extern u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */extern u64 __read_mostly shadow_user_mask;extern u64 __read_mostly shadow_accessed_mask;extern u64 __read_mostly shadow_dirty_mask; -extern u64 __read_mostly shadow_mmio_value; -extern u64 __read_mostly shadow_mmio_mask; +extern u64 __read_mostly shadow_default_mmio_mask;extern u64 __read_mostly shadow_mmio_access_mask;extern u64 __read_mostly shadow_present_mask;extern u64 __read_mostly shadow_me_mask;
arch/x86/kvm/mmu/spte.h extern u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask; · line 234
Quoted 10 lines
arch/x86/kvm/mmu/spte.h +3
-3@@ -229,10 +228,10 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask;*/ extern u8 __read_mostly shadow_phys_bits; -static inline bool is_mmio_spte (u64 spte) +static inline bool is_mmio_spte (struct kvm *kvm,u64 spte) { -return (spte & shadow_mmio_mask) == shadow_mmio_value && -likely (shadow_mmio_value); +return (spte & kvm->arch.shadow_mmio_mask) == kvm->arch.shadow_mmio_value && +likely (kvm->arch.shadow_mmio_value ||kvm_gfn_stolen_mask (kvm));
I don't like using kvm_gfn_stolen_mask() to check whether SPTE is MMIO. kvm_gfn_stolen_mask() really doesn't imply anything regarding to setting up the value of MMIO SPTE. At least, I guess we can use some is_protected_vm() sort of things since it implies guest memory is protected therefore legacy way handling of MMIO doesn't work (i.e. you cannot parse MMIO instruction).
Quoted 97 lines
arch/x86/kvm/mmu/spte.h +0
-0}static inline bool is_shadow_present_pte (u64 pte)arch/x86/kvm/mmu/tdp_mmu.c +3
-3@@ -447,8 +447,8 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,* impact the guest since both the former and current SPTEs * are nonpresent. */ -if (WARN_ON (!is_mmio_spte (old_spte) && - !is_mmio_spte (new_spte) && +if (WARN_ON (!is_mmio_spte (kvm, old_spte) && + !is_mmio_spte (kvm, new_spte) && !is_removed_spte (new_spte)))pr_err ("Unexpected SPTE change! Nonpresent SPTEs\n" "should not be replaced with another,\n"@@ -927,7 +927,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, }/* If a MMIO SPTE is installed, the MMIO will need to be emulated. */ -if (unlikely (is_mmio_spte (new_spte))) { +if (unlikely (is_mmio_spte (vcpu->kvm, new_spte))) {trace_mark_mmio_spte (rcu_dereference (iter->sptep), iter->gfn, new_spte); ret =RET_PF_EMULATE ;arch/x86/kvm/svm/svm.c +1
-1@@ -4704,7 +4704,7 @@ static __init void svm_adjust_mmio_mask(void)*/ mask = (mask_bit <52 ) ?rsvd_bits (mask_bit,51 ) |PT_PRESENT_MASK :0 ; -kvm_mmu_set_mmio_spte_mask (mask, mask,PT_WRITABLE_MASK |PT_USER_MASK ); +kvm_mmu_set_default_mmio_spte_mask (mask); }static __init void svm_set_cpu_caps (void )arch/x86/kvm/vmx/main.c +5
-2@@ -23,9 +23,12 @@ static __init int vt_hardware_setup(void)tdx_hardware_setup (&vt_x86_ops); -if (enable_ept) +if (enable_ept) { +const u64 init_value = enable_tdx ?VMX_EPT_SUPPRESS_VE_BIT :0ull ;kvm_mmu_set_ept_masks (enable_ept_ad_bits, -cpu_has_vmx_ept_execute_only ()); +cpu_has_vmx_ept_execute_only (), init_value); +kvm_mmu_set_spte_init_value (init_value); + }return 0 ; }arch/x86/kvm/vmx/tdx.c +1
-1@@ -11,7 +11,7 @@#undef pr_fmt#define pr_fmt (fmt) "tdx: " fmt -static bool __read_mostly enable_tdx = true; +bool __read_mostly enable_tdx = true;module_param_named (tdx, enable_tdx, bool,0644 );#define TDX_MAX_NR_CPUID_CONFIGS \arch/x86/kvm/vmx/tdx.h +2
-0@@ -6,6 +6,7 @@#include "tdx_ops.h" +extern bool __read_mostly enable_tdx;int tdx_module_setup (void );struct tdx_td_page {@@ -166,6 +167,7 @@ static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 fiel }#else +#define enable_tdx falsestatic inline int tdx_module_setup (void ) {return -ENODEV ; };struct kvm_tdx ;arch/x86/kvm/vmx/vmx.c +0
-0
arch/x86/kvm/vmx/vmx.c int vmx_vm_init(struct kvm *kvm) · line 7074
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +7
-0@@ -7065,6 +7065,14 @@ int vmx_vm_init(struct kvm *kvm)if (!ple_gap) kvm->arch.pause_in_guest = true; +/* +* EPT Misconfigurations can be generated if the value of bits 2:0 +* of an EPT paging-structure entry is 110b (write/execute). +*/ +if (enable_ept) +kvm_mmu_set_mmio_spte_mask (kvm,VMX_EPT_MISCONFIG_WX_VALUE , +VMX_EPT_MISCONFIG_WX_VALUE ,0 );
Should be:
kvm_mmu_set_mmio_spte_mask(kvm, VMX_EPT_MISCONFIG_WX_VALUE, VMX_EPT_RWX_MASK, 0);
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +1
-0+if (boot_cpu_has (X86_BUG_L1TF ) && enable_ept) {switch (l1tf_mitigation) {case L1TF_MITIGATION_OFF :
Re: [RFC PATCH v5 042/104] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
Quoted 25 lines · Kai Huang
arch/x86/kvm/mmu/spte.c void kvm_mmu_reset_all_pte_masks(void) · line 386
Quoted 13 lines
void
kvm_mmu_reset_all_pte_masks(void)
{
u8low_phys_bits;arch/x86/kvm/mmu/spte.c +0
-1-u64 mask;
shadow_phys_bits=kvm_get_shadow_phys_bits();arch/x86/kvm/mmu/spte.c +1
-1@@ -389,9 +383,13 @@ void kvm_mmu_reset_all_pte_masks(void)* PTEs and so the reserved PA approach must be disabled. */ if (shadow_phys_bits < 52) - mask =BIT_ULL (51 ) |PT_PRESENT_MASK ; + shadow_default_mmio_mask =BIT_ULL (51 ) |PT_PRESENT_MASK ;Hmm... Not related to this patch, but it seems there's a bug here. On a MKTME
enabled system (but not TDX) with 52 physical bits, theshadow_phys_bitswill be
set to < 52 (depending on how many MKTME KeyIDs are configured by BIOS). In this case, bit 51 is set, but actually bit 51 isn't a reserved bit in this case.
Instead, it is a MKTME KeyID bit. Therefore, above setting won't cause #PF, but
will use a non-zero MKTME keyID to access the physical address.
Quoted 1 line
Paolo/Sean, any comments here?
After looking at the code more carefully, this is not correct. shadow_phys_bits will be 52 on a MKTME-enabled system. Please ignore this.
Signature · 2 lines
Thanks, -Kai
Re: [RFC PATCH v5 042/104] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
In reply to Kai HuangQuoted 50 lines · Kai Huang
Quoted 50 lines
arch/x86/kvm/mmu/spte.c +8
-7@@ -29,8 +29,7 @@ u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */u64 __read_mostly shadow_user_mask;u64 __read_mostly shadow_accessed_mask;u64 __read_mostly shadow_dirty_mask; -u64 __read_mostly shadow_mmio_value; -u64 __read_mostly shadow_mmio_mask; +u64 __read_mostly shadow_default_mmio_mask;u64 __read_mostly shadow_mmio_access_mask;u64 __read_mostly shadow_present_mask;u64 __read_mostly shadow_me_mask;@@ -59,10 +58,11 @@ u64 make_mmio_spte(struct kvm_vcpu *vcpu, u64 gfn, unsigned int access)u64 spte =generation_mmio_spte_mask (gen);u64 gpa = gfn <<PAGE_SHIFT ; -WARN_ON_ONCE (!shadow_mmio_value); +WARN_ON_ONCE (!vcpu->kvm->arch.shadow_mmio_value && + !kvm_gfn_stolen_mask (vcpu->kvm)); access &= shadow_mmio_access_mask; - spte |= shadow_mmio_value | access; + spte |= vcpu->kvm->arch.shadow_mmio_value | access; spte |= gpa | shadow_nonpresent_or_rsvd_mask; spte |= (gpa & shadow_nonpresent_or_rsvd_mask) <<SHADOW_NONPRESENT_OR_RSVD_MASK_LEN ;@@ -279,7 +279,8 @@ u64 mark_spte_for_access_track(u64 spte)return spte; } -void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask) +void kvm_mmu_set_mmio_spte_mask (struct kvm *kvm,u64 mmio_value,u64 mmio_mask, +u64 access_mask) {BUG_ON ((u64 )(unsigned )access_mask != access_mask);WARN_ON (mmio_value & shadow_nonpresent_or_rsvd_lower_gfn_mask);@@ -308,39 +309,32 @@ void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask) WARN_ON(mmio_value && (REMOVED_SPTE & mmio_mask) == mmio_value)) mmio_value =0 ; - shadow_mmio_value = mmio_value; - shadow_mmio_mask = mmio_mask; + kvm->arch.shadow_mmio_value = mmio_value; + kvm->arch.shadow_mmio_mask = mmio_mask; shadow_mmio_access_mask = access_mask; }EXPORT_SYMBOL_GPL (kvm_mmu_set_mmio_spte_mask);
Quoted 14 lines
arch/x86/kvm/mmu/spte.c void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask) · line 326
Quoted 11 lines
arch/x86/kvm/mmu/spte.c +3
-2-void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only) +void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only,u64 init_value) { shadow_user_mask =VMX_EPT_READABLE_MASK ; shadow_accessed_mask = has_ad_bits ?VMX_EPT_ACCESS_BIT :0ull ; shadow_dirty_mask = has_ad_bits ?VMX_EPT_DIRTY_BIT :0ull ; shadow_nx_mask =0ull ; shadow_x_mask =VMX_EPT_EXECUTABLE_MASK ; - shadow_present_mask = has_exec_only ?0ull :VMX_EPT_READABLE_MASK ; + shadow_present_mask = + (has_exec_only ?0ull :VMX_EPT_READABLE_MASK ) | init_value;This change doesn't seem make any sense. Why should "Suppress #VE" bit be set for a present PTE?
Because W or NX violation also needs #VE. Although the name uses present, it's actually readable.
Quoted 67 lines
Quoted 23 lines
arch/x86/kvm/mmu/spte.c +0
-8shadow_acc_track_mask =VMX_EPT_RWX_MASK ; shadow_me_mask =0ull ; shadow_host_writable_mask =EPT_SPTE_HOST_WRITABLE ; shadow_mmu_writable_mask =EPT_SPTE_MMU_WRITABLE ; - -/* -* EPT Misconfigurations are generated if the value of bits 2:0 -* of an EPT paging-structure entry is 110b (write/execute). -*/ -kvm_mmu_set_mmio_spte_mask (VMX_EPT_MISCONFIG_WX_VALUE , -VMX_EPT_RWX_MASK ,0 ); }EXPORT_SYMBOL_GPL (kvm_mmu_set_ept_masks);void kvm_mmu_reset_all_pte_masks (void ) {u8 low_phys_bits; -u64 mask; shadow_phys_bits =kvm_get_shadow_phys_bits ();arch/x86/kvm/mmu/spte.c void kvm_mmu_reset_all_pte_masks(void) · line 386
Quoted 5 lines
arch/x86/kvm/mmu/spte.c +1
-1@@ -389,9 +383,13 @@ void kvm_mmu_reset_all_pte_masks(void)* PTEs and so the reserved PA approach must be disabled. */ if (shadow_phys_bits <52 ) - mask =BIT_ULL (51 ) |PT_PRESENT_MASK ; + shadow_default_mmio_mask =BIT_ULL (51 ) |PT_PRESENT_MASK ;Hmm... Not related to this patch, but it seems there's a bug here. On a MKTME enabled system (but not TDX) with 52 physical bits, the
shadow_phys_bitswill be set to < 52 (depending on how many MKTME KeyIDs are configured by BIOS). In this case, bit 51 is set, but actually bit 51 isn't a reserved bit in this case. Instead, it is a MKTME KeyID bit. Therefore, above setting won't cause #PF, but will use a non-zero MKTME keyID to access the physical address.Paolo/Sean, any comments here?
Quoted 29 lines
arch/x86/kvm/mmu/spte.c +6
-2else - mask =0 ; + shadow_default_mmio_mask =0 ; +} -kvm_mmu_set_mmio_spte_mask (mask, mask,ACC_WRITE_MASK |ACC_USER_MASK ); +void kvm_mmu_set_default_mmio_spte_mask (u64 mask) +{ + shadow_default_mmio_mask = mask; } +EXPORT_SYMBOL_GPL (kvm_mmu_set_default_mmio_spte_mask);arch/x86/kvm/mmu/spte.h +1
-2@@ -165,8 +165,7 @@ extern u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */extern u64 __read_mostly shadow_user_mask;extern u64 __read_mostly shadow_accessed_mask;extern u64 __read_mostly shadow_dirty_mask; -extern u64 __read_mostly shadow_mmio_value; -extern u64 __read_mostly shadow_mmio_mask; +extern u64 __read_mostly shadow_default_mmio_mask;extern u64 __read_mostly shadow_mmio_access_mask;extern u64 __read_mostly shadow_present_mask;extern u64 __read_mostly shadow_me_mask;@@ -229,10 +228,10 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask;*/ extern u8 __read_mostly shadow_phys_bits;
Quoted 13 lines
arch/x86/kvm/mmu/spte.h extern u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask; · line 234
Quoted 7 lines
arch/x86/kvm/mmu/spte.h +3
-3-static inline bool is_mmio_spte (u64 spte) +static inline bool is_mmio_spte (struct kvm *kvm,u64 spte) { -return (spte & shadow_mmio_mask) == shadow_mmio_value && -likely (shadow_mmio_value); +return (spte & kvm->arch.shadow_mmio_mask) == kvm->arch.shadow_mmio_value && +likely (kvm->arch.shadow_mmio_value ||kvm_gfn_stolen_mask (kvm));I don't like using
kvm_gfn_stolen_mask()to check whether SPTE is MMIO.kvm_gfn_stolen_mask()really doesn't imply anything regarding to setting up the value of MMIO SPTE. At least, I guess we can use someis_protected_vm()sort of things since it implies guest memory is protected therefore legacy way handling of MMIO doesn't work (i.e. you cannot parse MMIO instruction).
As discussed in other thread, let's rename those functions.
Quoted 18 lines
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +0
-0arch/x86/kvm/vmx/vmx.c int vmx_vm_init(struct kvm *kvm) · line 7074
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +7
-0@@ -7065,6 +7065,14 @@ int vmx_vm_init(struct kvm *kvm)if (!ple_gap) kvm->arch.pause_in_guest = true; +/* +* EPT Misconfigurations can be generated if the value of bits 2:0 +* of an EPT paging-structure entry is 110b (write/execute). +*/ +if (enable_ept) +kvm_mmu_set_mmio_spte_mask (kvm,VMX_EPT_MISCONFIG_WX_VALUE , +VMX_EPT_MISCONFIG_WX_VALUE ,0 );Should be:
Quoted 2 lines
kvm_mmu_set_mmio_spte_mask(kvm, VMX_EPT_MISCONFIG_WX_VALUE, VMX_EPT_RWX_MASK, 0);
Thanks for catching it. It's fixed in github repo.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [RFC PATCH v5 042/104] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
Quoted 10 lines · Isaku Yamahata
Quoted 9 lines
Quoted 5 lines
(quoted diff) +0
-1- shadow_present_mask = has_exec_only ? 0ull :
VMX_EPT_READABLE_MASK;(quoted diff) +2
-0+ shadow_present_mask = + (has_exec_only ? 0ull : VMX_EPT_READABLE_MASK) |
init_value;This change doesn't seem make any sense. Why should "Suppress #VE" bit be set
for a present PTE?
Quoted 3 lines
Because W or NX violation also needs #VE. Although the name uses present, it's
actually readable.
Yeah I forgot this. Thanks!
Signature · 2 lines
Thanks, -Kai
RFC PATCH v5 43/104 KVM: TDX: Add load_mmu_pgd method for TDX
In reply to [email protected]From: Sean Christopherson <[email protected]>
For virtual IO, the guest TD shares guest pages with VMM without encryption. Shared EPT is used to map guest pages in unprotected way.
Add the VMCS field encoding for the shared EPTP, which will be used by TDX to have separate EPT walks for private GPAs (existing EPTP) versus shared GPAs (new shared EPTP).
Set shared EPT pointer value for the TDX guest to initialize TDX MMU.
--- arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/vmx/main.c | 11 ++++++++++- arch/x86/kvm/vmx/tdx.c | 5 +++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 20 insertions(+), 1 deletion(-)
arch/x86/include/asm/vmx.h +1 -0
@@ -221,6 +221,7 @@ enum vmcs_field {ENCLS_EXITING_BITMAP_HIGH =0x0000202F ,TSC_MULTIPLIER =0x00002032 ,TSC_MULTIPLIER_HIGH =0x00002033 , +SHARED_EPT_POINTER =0x0000203C ,GUEST_PHYSICAL_ADDRESS =0x00002400 ,GUEST_PHYSICAL_ADDRESS_HIGH =0x00002401 , VMCS_LINK_POINTER = 0x00002800,
arch/x86/kvm/vmx/main.c +10 -1
@@ -89,6 +89,15 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa, +int pgd_level) +{ +if (is_td_vcpu (vcpu)) +return tdx_load_mmu_pgd (vcpu, root_hpa, pgd_level); + +vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); +} +static int vt_mem_enc_op (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -205,7 +214,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .write_tsc_offset = vmx_write_tsc_offset, .write_tsc_multiplier = vmx_write_tsc_multiplier, - .load_mmu_pgd = vmx_load_mmu_pgd, + .load_mmu_pgd = vt_load_mmu_pgd, .check_intercept = vmx_check_intercept, .handle_exit_irqoff = vmx_handle_exit_irqoff,
arch/x86/kvm/vmx/tdx.c +5 -0
@@ -496,6 +496,11 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) +{ +td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK ); +} +static int tdx_capabilities (struct kvm *kvm,struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -143,6 +143,8 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); + +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);#else static inline void tdx_pre_kvm_init (unsigned int *vcpu_size,unsigned int *vcpu_align,unsigned int *vm_size) {}
@@ -160,6 +162,8 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } + +static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {}#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 043/104] KVM: TDX: Add load_mmu_pgd method for TDX
Quoted 91 lines · On 3/4/22 20:48, [email protected] wrote:
From: Sean Christopherson <[email protected]>
For virtual IO, the guest TD shares guest pages with VMM without encryption. Shared EPT is used to map guest pages in unprotected way.
Add the VMCS field encoding for the shared EPTP, which will be used by TDX to have separate EPT walks for private GPAs (existing EPTP) versus shared GPAs (new shared EPTP).
Set shared EPT pointer value for the TDX guest to initialize TDX MMU.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/vmx.h | 1 + arch/x86/kvm/vmx/main.c | 11 ++++++++++- arch/x86/kvm/vmx/tdx.c | 5 +++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 20 insertions(+), 1 deletion(-)arch/x86/include/asm/vmx.h +1
-0@@ -221,6 +221,7 @@ enum vmcs_field {ENCLS_EXITING_BITMAP_HIGH =0x0000202F ,TSC_MULTIPLIER =0x00002032 ,TSC_MULTIPLIER_HIGH =0x00002033 , +SHARED_EPT_POINTER =0x0000203C ,GUEST_PHYSICAL_ADDRESS =0x00002400 ,GUEST_PHYSICAL_ADDRESS_HIGH =0x00002401 , VMCS_LINK_POINTER = 0x00002800,arch/x86/kvm/vmx/main.c +10
-1@@ -89,6 +89,15 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa, +int pgd_level) +{ +if (is_td_vcpu (vcpu)) +return tdx_load_mmu_pgd (vcpu, root_hpa, pgd_level); + +vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); +} +static int vt_mem_enc_op (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))@@ -205,7 +214,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .write_tsc_offset = vmx_write_tsc_offset, .write_tsc_multiplier = vmx_write_tsc_multiplier, - .load_mmu_pgd = vmx_load_mmu_pgd, + .load_mmu_pgd = vt_load_mmu_pgd, .check_intercept = vmx_check_intercept, .handle_exit_irqoff = vmx_handle_exit_irqoff,arch/x86/kvm/vmx/tdx.c +5
-0@@ -496,6 +496,11 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) +{ +td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK ); +} +static int tdx_capabilities (struct kvm *kvm,struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;arch/x86/kvm/vmx/x86_ops.h +2
-0@@ -143,6 +143,8 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); + +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);#else static inline void tdx_pre_kvm_init (unsigned int *vcpu_size,unsigned int *vcpu_align,unsigned int *vm_size) {}
arch/x86/kvm/vmx/x86_ops.h static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {} · line 169
Quoted 8 lines
arch/x86/kvm/vmx/x86_ops.h +2
-0@@ -160,6 +162,8 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } + +static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {}#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
RFC PATCH v5 44/104 The start of TDX KVM patch series: KVM TDP MMU hooks
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of KVM TDP MMU hooks.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -25,6 +25,6 @@ Patch Layer status * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA stolen bits: Applied -* KVM TDP refactoring for TDX: Applying -* KVM TDP MMU hooks: Not yet +* KVM TDP refactoring for TDX: Applied +* KVM TDP MMU hooks: Applying * KVM TDP MMU MapGPA: Not yet
Signature · 1 line
2.25.1
RFC PATCH v5 45/104 KVM: x86/tdp_mmu: make REMOVED_SPTE include shadow_initial value
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDP MMU uses REMOVED_SPTE = 0x5a0ULL as special constant to indicate the intermediate value to indicate one thread is operating on it and the value should be semi-arbitrary value. For TDX (more correctly to use #VE), the value should include suppress #VE value which is shadow_init_value.
Define SHADOW_REMOVED_SPTE as shadow_init_value | REMOVED_SPTE, and replace REMOVED_SPTE with SHADOW_REMOVED_SPTE to use suppress #VE bit properly for TDX.
--- arch/x86/kvm/mmu/spte.h | 14 ++++++++++++-- arch/x86/kvm/mmu/tdp_mmu.c | 23 ++++++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-)
arch/x86/kvm/mmu/spte.h +12 -2
@@ -194,7 +194,9 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;* If a thread running without exclusive control of the MMU lock must perform a * multi-part operation on an SPTE, it can set the SPTE to REMOVED_SPTE as a * non-present intermediate value. Other threads which encounter this value -* should not modify the SPTE. +* should not modify the SPTE. When TDX is enabled, shadow_init_value, which +* is "suppress #VE" bit set, is also set to removed SPTE, because TDX module +* always enables "EPT violation #VE". * * Use a semi-arbitrary value that doesn't set RWX bits, i.e. is not-present on * bot AMD and Intel CPUs, and doesn't set PFN bits, i.e. doesn't create a L1TF
@@ -207,9 +209,17 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;/* Removed SPTEs must not be misconstrued as shadow present PTEs. */ static_assert (!(REMOVED_SPTE &SPTE_MMU_PRESENT_MASK )); +/* +* See above comment around REMOVED_SPTE. SHADOW_REMOVED_SPTE is the actual +* intermediate value set to the removed SPET. When TDX is enabled, it sets +* the "suppress #VE" bit, otherwise it's REMOVED_SPTE. +*/ +extern u64 __read_mostly shadow_init_value; +#define SHADOW_REMOVED_SPTE (shadow_init_value | REMOVED_SPTE) +static inline bool is_removed_spte (u64 spte) { -return spte ==REMOVED_SPTE ; +return spte ==SHADOW_REMOVED_SPTE ; }/*
arch/x86/kvm/mmu/tdp_mmu.c +16 -7
@@ -338,7 +338,7 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,* value to the removed SPTE value. */ for (;;) { - old_child_spte =xchg (sptep,REMOVED_SPTE ); + old_child_spte =xchg (sptep,SHADOW_REMOVED_SPTE );if (!is_removed_spte (old_child_spte))break ;cpu_relax ();
@@ -365,10 +365,10 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,* the two branches consistent and simplifies * the function. */ -WRITE_ONCE (*sptep,REMOVED_SPTE ); +WRITE_ONCE (*sptep,SHADOW_REMOVED_SPTE ); }handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, - old_child_spte,REMOVED_SPTE , level, + old_child_spte,SHADOW_REMOVED_SPTE , level, shared); }
@@ -537,7 +537,7 @@ static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm,* immediately installing a present entry in its place * before the TLBs are flushed. */ -if (!tdp_mmu_set_spte_atomic (kvm, iter,REMOVED_SPTE )) +if (!tdp_mmu_set_spte_atomic (kvm, iter,SHADOW_REMOVED_SPTE ))return false; kvm_flush_remote_tlbs_with_address(kvm, iter->gfn,
@@ -550,8 +550,16 @@ static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm,* special removed SPTE value. No bookkeeping is needed * here since the SPTE is going from non-present * to non-present. +* +* Set non-present value to shadow_init_value, rather than 0. +* It is because when TDX is enabled, TDX module always +* enables "EPT-violation #VE", so KVM needs to set +* "suppress #VE" bit in EPT table entries, in order to get +* real EPT violation, rather than TDVMCALL. KVM sets +* shadow_init_value (which sets "suppress #VE" bit) so it +* can be set when EPT table entries are zapped. */ -WRITE_ONCE (*rcu_dereference (iter->sptep),0 ); +WRITE_ONCE (*rcu_dereference (iter->sptep), shadow_init_value);return true; }
@@ -748,7 +756,8 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,continue ;if (!shared) { -tdp_mmu_set_spte (kvm, &iter,0 ); +/* see comments in tdp_mmu_zap_spte_atomic() */ +tdp_mmu_set_spte (kvm, &iter, shadow_init_value); flush = true; }else if (!tdp_mmu_zap_spte_atomic (kvm, &iter)) {/*
@@ -1135,7 +1144,7 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,* invariant that the PFN of a present * leaf SPTE can never change. * See __handle_changed_spte(). */ -tdp_mmu_set_spte (kvm, iter,0 ); +tdp_mmu_set_spte (kvm, iter, shadow_init_value);if (!pte_write (range->pte)) { new_spte = kvm_mmu_changed_pte_notifier_make_spte(iter->old_spte,
Signature · 1 line
2.25.1
Paolo Bonzini
maintainerRe: [RFC PATCH v5 045/104] KVM: x86/tdp_mmu: make REMOVED_SPTE include shadow_initial value
Quoted 33 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDP MMU uses
REMOVED_SPTE=0x5a0ULLas special constant to indicate the intermediate value to indicate one thread is operating on it and the value should be semi-arbitrary value. For TDX (more correctly to use #VE), the value should include suppress #VE value which isshadow_init_value.Define
SHADOW_REMOVED_SPTEasshadow_init_value|REMOVED_SPTE, and replaceREMOVED_SPTEwithSHADOW_REMOVED_SPTEto use suppress #VE bit properly for TDX.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/spte.h | 14 ++++++++++++-- arch/x86/kvm/mmu/tdp_mmu.c | 23 ++++++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-)arch/x86/kvm/mmu/spte.h +3
-1@@ -194,7 +194,9 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;* If a thread running without exclusive control of the MMU lock must perform a * multi-part operation on an SPTE, it can set the SPTE to REMOVED_SPTE as a * non-present intermediate value. Other threads which encounter this value -* should not modify the SPTE. +* should not modify the SPTE. When TDX is enabled, shadow_init_value, which +* is "suppress #VE" bit set, is also set to removed SPTE, because TDX module +* always enables "EPT violation #VE". * * Use a semi-arbitrary value that doesn't set RWX bits, i.e. is not-present on * bot AMD and Intel CPUs, and doesn't set PFN bits, i.e. doesn't create a L1TF
arch/x86/kvm/mmu/spte.h extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask; · line 218
Quoted 10 lines
arch/x86/kvm/mmu/spte.h +7
-0@@ -207,9 +209,17 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;/* Removed SPTEs must not be misconstrued as shadow present PTEs. */ static_assert (!(REMOVED_SPTE &SPTE_MMU_PRESENT_MASK )); +/* +* See above comment around REMOVED_SPTE. SHADOW_REMOVED_SPTE is the actual +* intermediate value set to the removed SPET. When TDX is enabled, it sets +* the "suppress #VE" bit, otherwise it's REMOVED_SPTE. +*/ +extern u64 __read_mostly shadow_init_value; +#define SHADOW_REMOVED_SPTE (shadow_init_value | REMOVED_SPTE)
Please rename the existing REMOVED_SPTE to REMOVED_SPTE_MASK, and call this simply REMOVED_SPTE. This also makes the patch smaller.
Paolo
Quoted 75 lines
arch/x86/kvm/mmu/spte.h +0
-0}/* arch/x86/kvm/mmu/tdp_mmu.c +16
-7@@ -338,7 +338,7 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,* value to the removed SPTE value. */ for (;;) { - old_child_spte =xchg (sptep,REMOVED_SPTE ); + old_child_spte =xchg (sptep,SHADOW_REMOVED_SPTE );if (!is_removed_spte (old_child_spte))break ;cpu_relax ();@@ -365,10 +365,10 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,* the two branches consistent and simplifies * the function. */ -WRITE_ONCE (*sptep,REMOVED_SPTE ); +WRITE_ONCE (*sptep,SHADOW_REMOVED_SPTE ); }handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, - old_child_spte,REMOVED_SPTE , level, + old_child_spte,SHADOW_REMOVED_SPTE , level, shared); }@@ -537,7 +537,7 @@ static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm,* immediately installing a present entry in its place * before the TLBs are flushed. */ -if (!tdp_mmu_set_spte_atomic (kvm, iter,REMOVED_SPTE )) +if (!tdp_mmu_set_spte_atomic (kvm, iter,SHADOW_REMOVED_SPTE ))return false; kvm_flush_remote_tlbs_with_address(kvm, iter->gfn,@@ -550,8 +550,16 @@ static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm,* special removed SPTE value. No bookkeeping is needed * here since the SPTE is going from non-present * to non-present. +* +* Set non-present value to shadow_init_value, rather than 0. +* It is because when TDX is enabled, TDX module always +* enables "EPT-violation #VE", so KVM needs to set +* "suppress #VE" bit in EPT table entries, in order to get +* real EPT violation, rather than TDVMCALL. KVM sets +* shadow_init_value (which sets "suppress #VE" bit) so it +* can be set when EPT table entries are zapped. */ -WRITE_ONCE (*rcu_dereference (iter->sptep),0 ); +WRITE_ONCE (*rcu_dereference (iter->sptep), shadow_init_value);return true; }@@ -748,7 +756,8 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,continue ;if (!shared) { -tdp_mmu_set_spte (kvm, &iter,0 ); +/* see comments in tdp_mmu_zap_spte_atomic() */ +tdp_mmu_set_spte (kvm, &iter, shadow_init_value); flush = true; }else if (!tdp_mmu_zap_spte_atomic (kvm, &iter)) {/* @@ -1135,7 +1144,7 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,* invariant that the PFN of a present * leaf SPTE can never change. * See __handle_changed_spte(). */ -tdp_mmu_set_spte (kvm, iter,0 ); +tdp_mmu_set_spte (kvm, iter, shadow_init_value);if (!pte_write (range->pte)) { new_spte = kvm_mmu_changed_pte_notifier_make_spte(iter->old_spte,
Sean Christopherson
maintainerRe: [RFC PATCH v5 045/104] KVM: x86/tdp_mmu: make REMOVED_SPTE include shadow_initial value
Quoted 14 lines · Paolo Bonzini
arch/x86/kvm/mmu/spte.h extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask; · line 217
Quoted 10 lines · On 3/4/22 20:49, [email protected] wrote:
arch/x86/kvm/mmu/spte.h +7
-0@@ -207,9 +209,17 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;/* Removed SPTEs must not be misconstrued as shadow present PTEs. */ static_assert (!(REMOVED_SPTE &SPTE_MMU_PRESENT_MASK )); +/* +* See above comment around REMOVED_SPTE. SHADOW_REMOVED_SPTE is the actual +* intermediate value set to the removed SPET. When TDX is enabled, it sets +* the "suppress #VE" bit, otherwise it's REMOVED_SPTE. +*/ +extern u64 __read_mostly shadow_init_value; +#define SHADOW_REMOVED_SPTE (shadow_init_value | REMOVED_SPTE)Please rename the existing
REMOVED_SPTEtoREMOVED_SPTE_MASK, and call this simplyREMOVED_SPTE. This also makes the patch smaller.
Can we name it either __REMOVE_SPTE or REMOVED_SPTE_VAL? It's most definitely not a mask, it's a full value, e.g. spte |= REMOVED_SPTE_MASK is completely wrong.
Other than that, 100% agree with avoiding churn.
Paolo Bonzini
maintainerRe: [RFC PATCH v5 045/104] KVM: x86/tdp_mmu: make REMOVED_SPTE include shadow_initial value
Quoted 4 lines · Sean Christopherson
Quoted 2 lines
Please rename the existing
REMOVED_SPTEtoREMOVED_SPTE_MASK, and call this simplyREMOVED_SPTE. This also makes the patch smaller.Can we name it either
__REMOVE_SPTEorREMOVED_SPTE_VAL? It's most definitely not a mask, it's a full value, e.g. spte |=REMOVED_SPTE_MASKis completely wrong.
REMOVED_SPTE_VAL is fine.
Paolo
Re: [RFC PATCH v5 045/104] KVM: x86/tdp_mmu: make REMOVED_SPTE include shadow_initial value
In reply to [email protected]Quoted 10 lines · On Fri, 2022-03-04 at 11:49 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDP MMU uses
REMOVED_SPTE=0x5a0ULLas special constant to indicate the intermediate value to indicate one thread is operating on it and the value should be semi-arbitrary value. For TDX (more correctly to use #VE), the value should include suppress #VE value which isshadow_init_value.Define
SHADOW_REMOVED_SPTEasshadow_init_value|REMOVED_SPTE, and replaceREMOVED_SPTEwithSHADOW_REMOVED_SPTEto use suppress #VE bit properly for TDX.
Like we discussed, this patch should be merged with patch "KVM: x86/mmu: Allow non-zero init value for shadow PTE".
Signature · 2 lines
Thanks, -Kai
RFC PATCH v5 46/104 KVM: x86/tdp_mmu: refactor kvm_tdp_mmu_map()
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Factor out non-leaf SPTE population logic from kvm_tdp_mmu_map(). MapGPA hypercall needs to populate non-leaf SPTE to record which GPA, private or shared, is allowed in the leaf EPT entry.
--- arch/x86/kvm/mmu/tdp_mmu.c | 48 ++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 18 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +30 -18
@@ -955,6 +955,31 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,return ret; } +static bool tdp_mmu_populate_nonleaf ( +struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool account_nx) +{ +struct kvm_mmu_page *sp; +u64 *child_pt; +u64 new_spte; + +WARN_ON (is_shadow_present_pte (iter->old_spte)); +WARN_ON (is_removed_spte (iter->old_spte)); + + sp =alloc_tdp_mmu_page (vcpu, iter->gfn, iter->level -1 ); + child_pt = sp->spt; + + new_spte =make_nonleaf_spte (child_pt, !shadow_accessed_mask); + +if (!tdp_mmu_set_spte_atomic (vcpu->kvm, iter, new_spte)) { +tdp_mmu_free_sp (sp); +return false; + } + +tdp_mmu_link_page (vcpu->kvm, sp, account_nx); +trace_kvm_mmu_get_page (sp, true); +return true; +} +/* * Handle a TDP page fault (NPT/EPT violation/misconfiguration) by installing * page tables and SPTEs to translate the faulting guest physical address.
@@ -963,9 +988,6 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) {struct kvm_mmu *mmu = vcpu->arch.mmu;struct tdp_iter iter; -struct kvm_mmu_page *sp; -u64 *child_pt; -u64 new_spte;int ret;kvm_mmu_hugepage_adjust (vcpu, fault);
@@ -1000,6 +1022,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) }if (!is_shadow_present_pte (iter.old_spte)) { +bool account_nx; +/* * If SPTE has been frozen by another thread, just * give up and retry, avoiding unnecessary page table
@@ -1008,22 +1032,10 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (is_removed_spte (iter.old_spte))break ; - sp =alloc_tdp_mmu_page (vcpu, iter.gfn, iter.level -1 ); - child_pt = sp->spt; - - new_spte =make_nonleaf_spte (child_pt, - !shadow_accessed_mask); - -if (tdp_mmu_set_spte_atomic (vcpu->kvm, &iter, new_spte)) { -tdp_mmu_link_page (vcpu->kvm, sp, - fault->huge_page_disallowed && - fault->req_level >= iter.level); - -trace_kvm_mmu_get_page (sp, true); - }else { -tdp_mmu_free_sp (sp); + account_nx = fault->huge_page_disallowed && + fault->req_level >= iter.level; +if (!tdp_mmu_populate_nonleaf (vcpu, &iter, account_nx))break ; - } } }
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 046/104] KVM: x86/tdp_mmu: refactor kvm_tdp_mmu_map()
Quoted 7 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Factor out non-leaf SPTE population logic from
kvm_tdp_mmu_map(). MapGPA hypercall needs to populate non-leaf SPTE to record which GPA, private or shared, is allowed in the leaf EPT entry.Signed-off-by Isaku Yamahata <[email protected]>
and feel free to rebase/resubmit this one, with subject "KVM: x86/tdp_mmu: extract tdp_mmu_populate_nonleaf()".
Paolo
Quoted 85 lines
--- arch/x86/kvm/mmu/tdp_mmu.c | 48 ++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 18 deletions(-)arch/x86/kvm/mmu/tdp_mmu.c +30
-18@@ -955,6 +955,31 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,return ret; } +static bool tdp_mmu_populate_nonleaf ( +struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool account_nx) +{ +struct kvm_mmu_page *sp; +u64 *child_pt; +u64 new_spte; + +WARN_ON (is_shadow_present_pte (iter->old_spte)); +WARN_ON (is_removed_spte (iter->old_spte)); + + sp =alloc_tdp_mmu_page (vcpu, iter->gfn, iter->level -1 ); + child_pt = sp->spt; + + new_spte =make_nonleaf_spte (child_pt, !shadow_accessed_mask); + +if (!tdp_mmu_set_spte_atomic (vcpu->kvm, iter, new_spte)) { +tdp_mmu_free_sp (sp); +return false; + } + +tdp_mmu_link_page (vcpu->kvm, sp, account_nx); +trace_kvm_mmu_get_page (sp, true); +return true; +} +/* * Handle a TDP page fault (NPT/EPT violation/misconfiguration) by installing * page tables and SPTEs to translate the faulting guest physical address. @@ -963,9 +988,6 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) {struct kvm_mmu *mmu = vcpu->arch.mmu;struct tdp_iter iter; -struct kvm_mmu_page *sp; -u64 *child_pt; -u64 new_spte;int ret;kvm_mmu_hugepage_adjust (vcpu, fault);@@ -1000,6 +1022,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) }if (!is_shadow_present_pte (iter.old_spte)) { +bool account_nx; +/* * If SPTE has been frozen by another thread, just * give up and retry, avoiding unnecessary page table @@ -1008,22 +1032,10 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (is_removed_spte (iter.old_spte))break ; - sp =alloc_tdp_mmu_page (vcpu, iter.gfn, iter.level -1 ); - child_pt = sp->spt; - - new_spte =make_nonleaf_spte (child_pt, - !shadow_accessed_mask); - -if (tdp_mmu_set_spte_atomic (vcpu->kvm, &iter, new_spte)) { -tdp_mmu_link_page (vcpu->kvm, sp, - fault->huge_page_disallowed && - fault->req_level >= iter.level); - -trace_kvm_mmu_get_page (sp, true); - }else { -tdp_mmu_free_sp (sp); + account_nx = fault->huge_page_disallowed && + fault->req_level >= iter.level; +if (!tdp_mmu_populate_nonleaf (vcpu, &iter, account_nx))break ; - } } }
RFC PATCH v5 47/104 KVM: x86/mmu: add a private pointer to struct kvm_mmu_page
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add a private pointer to kvm_mmu_page for private EPT.
To resolve KVM page fault on private GPA, it will allocate additional page for Secure EPT in addition to private EPT. Add memory allocator for it and topup its memory allocator before resolving KVM page fault similar to shared EPT page. Allocation of those memory will be done for TDP MMU by alloc_tdp_mmu_page(). Freeing those memory will be done for TDP MMU on behalf of kvm_tdp_mmu_zap_all() called by kvm_mmu_zap_all(). Private EPT page needs to carry one more page used for Secure EPT in addition to the private EPT page. Add private pointer to struct kvm_mmu_page for that purpose and Add helper functions to allocate/free a page for Secure EPT. Also add helper functions to check if a given kvm_mmu_page is private.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 9 ++++ arch/x86/kvm/mmu/mmu_internal.h | 84 +++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 3 ++ 4 files changed, 97 insertions(+)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -689,6 +689,7 @@ struct kvm_vcpu_arch {struct kvm_mmu_memory_cache mmu_shadow_page_cache;struct kvm_mmu_memory_cache mmu_gfn_array_cache;struct kvm_mmu_memory_cache mmu_page_header_cache; +struct kvm_mmu_memory_cache mmu_private_sp_cache;/* * QEMU userspace and the guest each have their own FPU state.
arch/x86/kvm/mmu/mmu.c +9 -0
@@ -758,6 +758,13 @@ static int mmu_topup_shadow_page_cache(struct kvm_vcpu *vcpu)struct kvm_mmu_memory_cache *mc = &vcpu->arch.mmu_shadow_page_cache;int start, end, i, r; +if (kvm_gfn_stolen_mask (vcpu->kvm)) { + r =kvm_mmu_topup_memory_cache (&vcpu->arch.mmu_private_sp_cache, +PT64_ROOT_MAX_LEVEL ); +if (r) +return r; + } +if (shadow_init_value) start =kvm_mmu_memory_cache_nr_free_objects (mc);
@@ -799,6 +806,7 @@ static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) {kvm_mmu_free_memory_cache (&vcpu->arch.mmu_pte_list_desc_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_shadow_page_cache); +kvm_mmu_free_memory_cache (&vcpu->arch.mmu_private_sp_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_gfn_array_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_page_header_cache); }
@@ -1791,6 +1799,7 @@ static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int directif (!direct) sp->gfns =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_gfn_array_cache);set_page_private (virt_to_page (sp->spt), (unsigned long )sp); +kvm_mmu_init_private_sp (sp);/* * active_mmu_pages must be a FIFO list, as kvm_zap_obsolete_pages()
arch/x86/kvm/mmu/mmu_internal.h +84 -0
96 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.c +3 -0
@@ -59,6 +59,8 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,static void tdp_mmu_free_sp (struct kvm_mmu_page *sp) { +if (is_private_sp (sp)) +kvm_mmu_free_private_sp (sp);free_page ((unsigned long )sp->spt);kmem_cache_free (mmu_page_header_cache, sp); }
@@ -184,6 +186,7 @@ static struct kvm_mmu_page *alloc_tdp_mmu_page(struct kvm_vcpu *vcpu, gfn_t gfn, sp->role.word =page_role_for_level (vcpu, level).word; sp->gfn = gfn; sp->tdp_mmu_page = true; +kvm_mmu_init_private_sp (sp);trace_kvm_mmu_get_page (sp, true);
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 047/104] KVM: x86/mmu: add a private pointer to struct kvm_mmu_page
Quoted 185 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Add a private pointer to
kvm_mmu_pagefor private EPT.To resolve KVM page fault on private GPA, it will allocate additional page for Secure EPT in addition to private EPT. Add memory allocator for it and topup its memory allocator before resolving KVM page fault similar to shared EPT page. Allocation of those memory will be done for TDP MMU by
alloc_tdp_mmu_page(). Freeing those memory will be done for TDP MMU on behalf ofkvm_tdp_mmu_zap_all()called bykvm_mmu_zap_all(). Private EPT page needs to carry one more page used for Secure EPT in addition to the private EPT page. Add private pointer to structkvm_mmu_pagefor that purpose and Add helper functions to allocate/free a page for Secure EPT. Also add helper functions to check if a givenkvm_mmu_pageis private.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 9 ++++ arch/x86/kvm/mmu/mmu_internal.h | 84 +++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 3 ++ 4 files changed, 97 insertions(+)arch/x86/include/asm/kvm_host.h +1
-0@@ -689,6 +689,7 @@ struct kvm_vcpu_arch {struct kvm_mmu_memory_cache mmu_shadow_page_cache;struct kvm_mmu_memory_cache mmu_gfn_array_cache;struct kvm_mmu_memory_cache mmu_page_header_cache; +struct kvm_mmu_memory_cache mmu_private_sp_cache;/* * QEMU userspace and the guest each have their own FPU state. arch/x86/kvm/mmu/mmu.c +9
-0@@ -758,6 +758,13 @@ static int mmu_topup_shadow_page_cache(struct kvm_vcpu *vcpu)struct kvm_mmu_memory_cache *mc = &vcpu->arch.mmu_shadow_page_cache;int start, end, i, r; +if (kvm_gfn_stolen_mask (vcpu->kvm)) { + r =kvm_mmu_topup_memory_cache (&vcpu->arch.mmu_private_sp_cache, +PT64_ROOT_MAX_LEVEL ); +if (r) +return r; + } +if (shadow_init_value) start =kvm_mmu_memory_cache_nr_free_objects (mc);@@ -799,6 +806,7 @@ static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) {kvm_mmu_free_memory_cache (&vcpu->arch.mmu_pte_list_desc_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_shadow_page_cache); +kvm_mmu_free_memory_cache (&vcpu->arch.mmu_private_sp_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_gfn_array_cache);kvm_mmu_free_memory_cache (&vcpu->arch.mmu_page_header_cache); }@@ -1791,6 +1799,7 @@ static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int directif (!direct) sp->gfns =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_gfn_array_cache);set_page_private (virt_to_page (sp->spt), (unsigned long )sp); +kvm_mmu_init_private_sp (sp);/* * active_mmu_pages must be a FIFO list, as kvm_zap_obsolete_pages() arch/x86/kvm/mmu/mmu_internal.h +84
-0@@ -53,6 +53,10 @@ struct kvm_mmu_page {u64 *spt;/* hold the gfn of each spte inside spt */ gfn_t *gfns; +#ifdef CONFIG_KVM_MMU_PRIVATE +/* associated private shadow page, e.g. SEPT page */ +void *private_sp; +#endif /* Currently serving as active root */ union {int root_count;@@ -104,6 +108,86 @@ static inline int kvm_mmu_page_as_id(struct kvm_mmu_page *sp)return kvm_mmu_role_as_id (sp->role); } +/* +* TDX vcpu allocates page for root Secure EPT page and assigns to CPU secure +* EPT pointer. KVM doesn't need to allocate and link to the secure EPT. +* Dummy value to make is_pivate_sp() return true. +*/ +#define KVM_MMU_PRIVATE_SP_ROOT ((void *)1) + +#ifdef CONFIG_KVM_MMU_PRIVATE +static inline bool is_private_sp (struct kvm_mmu_page *sp) +{ +return !!sp->private_sp; +} + +static inline bool is_private_spte (u64 *sptep) +{ +return is_private_sp (sptep_to_sp (sptep)); +} + +static inline void *kvm_mmu_private_sp (struct kvm_mmu_page *sp) +{ +return sp->private_sp; +} + +static inline void kvm_mmu_init_private_sp (struct kvm_mmu_page *sp) +{ + sp->private_sp =NULL ; +} + +/* Valid sp->role.level is required. */ +static inline void kvm_mmu_alloc_private_sp (struct kvm_vcpu *vcpu, +struct kvm_mmu_page *sp) +{ +if (vcpu->arch.mmu->shadow_root_level == sp->role.level) + sp->private_sp =KVM_MMU_PRIVATE_SP_ROOT ; +else + sp->private_sp = +kvm_mmu_memory_cache_alloc ( + &vcpu->arch.mmu_private_sp_cache); +/* +* Because mmu_private_sp_cache is topped up before staring kvm page +* fault resolving, the allocation above shouldn't fail. +*/ +WARN_ON_ONCE (!sp->private_sp); +} + +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) +{ +if (sp->private_sp !=KVM_MMU_PRIVATE_SP_ROOT ) +free_page ((unsigned long )sp->private_sp); +} +#else +static inline bool is_private_sp (struct kvm_mmu_page *sp) +{ +return false; +} + +static inline bool is_private_spte (u64 *sptep) +{ +return false; +} + +static inline void *kvm_mmu_private_sp (struct kvm_mmu_page *sp) +{ +return NULL ; +} + +static inline void kvm_mmu_init_private_sp (struct kvm_mmu_page *sp) +{ +} + +static inline void kvm_mmu_alloc_private_sp (struct kvm_vcpu *vcpu, +struct kvm_mmu_page *sp) +{ +} + +static inline void kvm_mmu_free_private_sp (struct kvm_mmu_page *sp) +{ +} +#endif +static inline bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp) {/* arch/x86/kvm/mmu/tdp_mmu.c +2
-0@@ -59,6 +59,8 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,static void tdp_mmu_free_sp (struct kvm_mmu_page *sp) { +if (is_private_sp (sp)) +kvm_mmu_free_private_sp (sp);free_page ((unsigned long )sp->spt);kmem_cache_free (mmu_page_header_cache, sp); }
arch/x86/kvm/mmu/tdp_mmu.c static struct kvm_mmu_page *alloc_tdp_mmu_page(struct kvm_vcpu *vcpu, gfn_t gfn, · line 192
Quoted 7 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-0@@ -184,6 +186,7 @@ static struct kvm_mmu_page *alloc_tdp_mmu_page(struct kvm_vcpu *vcpu, gfn_t gfn, sp->role.word =page_role_for_level (vcpu, level).word; sp->gfn = gfn; sp->tdp_mmu_page = true; +kvm_mmu_init_private_sp (sp);trace_kvm_mmu_get_page (sp, true);
Re: [RFC PATCH v5 047/104] KVM: x86/mmu: add a private pointer to struct kvm_mmu_page
In reply to [email protected]Quoted 40 lines · On Fri, 2022-03-04 at 11:49 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Add a private pointer to
kvm_mmu_pagefor private EPT.To resolve KVM page fault on private GPA, it will allocate additional page for Secure EPT in addition to private EPT. Add memory allocator for it and topup its memory allocator before resolving KVM page fault similar to shared EPT page. Allocation of those memory will be done for TDP MMU by
alloc_tdp_mmu_page(). Freeing those memory will be done for TDP MMU on behalf ofkvm_tdp_mmu_zap_all()called bykvm_mmu_zap_all(). Private EPT page needs to carry one more page used for Secure EPT in addition to the private EPT page. Add private pointer to structkvm_mmu_pagefor that purpose and Add helper functions to allocate/free a page for Secure EPT. Also add helper functions to check if a givenkvm_mmu_pageis private.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 9 ++++ arch/x86/kvm/mmu/mmu_internal.h | 84 +++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 3 ++ 4 files changed, 97 insertions(+)arch/x86/include/asm/kvm_host.h +1
-0@@ -689,6 +689,7 @@ struct kvm_vcpu_arch {struct kvm_mmu_memory_cache mmu_shadow_page_cache;struct kvm_mmu_memory_cache mmu_gfn_array_cache;struct kvm_mmu_memory_cache mmu_page_header_cache; +struct kvm_mmu_memory_cache mmu_private_sp_cache;/* * QEMU userspace and the guest each have their own FPU state. arch/x86/kvm/mmu/mmu.c +0
-0
arch/x86/kvm/mmu/mmu.c static int mmu_topup_shadow_page_cache(struct kvm_vcpu *vcpu) · line 761
Quoted 4 lines
arch/x86/kvm/mmu/mmu.c +1
-0@@ -758,6 +758,13 @@ static int mmu_topup_shadow_page_cache(struct kvm_vcpu *vcpu)struct kvm_mmu_memory_cache *mc = &vcpu->arch.mmu_shadow_page_cache;int start, end, i, r; +if (kvm_gfn_stolen_mask (vcpu->kvm)) {
Please get rid of kvm_gfn_stolen_mask().
Quoted 9 lines
arch/x86/kvm/mmu/mmu.c +6
-0+ r =kvm_mmu_topup_memory_cache (&vcpu->arch.mmu_private_sp_cache, +PT64_ROOT_MAX_LEVEL ); +if (r) +return r; + } +if (shadow_init_value) start =kvm_mmu_memory_cache_nr_free_objects (mc);
Paolo Bonzini
maintainerRe: [RFC PATCH v5 047/104] KVM: x86/mmu: add a private pointer to struct kvm_mmu_page
Quoted 3 lines · Kai Huang
arch/x86/kvm/mmu/mmu.c static int mmu_topup_shadow_page_cache(struct kvm_vcpu *vcpu) · line 761
Quoted 1 line
arch/x86/kvm/mmu/mmu.c +1
-0+if (kvm_gfn_stolen_mask (vcpu->kvm)) {Please get rid of
kvm_gfn_stolen_mask().
Kai, please follow the other reviews that I have posted in the last few days.
Paolo
Re: [RFC PATCH v5 047/104] KVM: x86/mmu: add a private pointer to struct kvm_mmu_page
Quoted 10 lines · Paolo Bonzini
Quoted 3 lines · Kai Huang
arch/x86/kvm/mmu/mmu.c static int mmu_topup_shadow_page_cache(struct kvm_vcpu *vcpu) · line 761
Quoted 1 line
arch/x86/kvm/mmu/mmu.c +1
-0+if (kvm_gfn_stolen_mask (vcpu->kvm)) {Please get rid of
kvm_gfn_stolen_mask().Kai, please follow the other reviews that I have posted in the last few days.
Paolo
Do you mean below reply?
"I think use of kvm_gfn_stolen_mask() should be minimized anyway. I would rename it to to kvm_{gfn,gpa}_private_mask and not return bool."
I also mean we should not use kvm_gfn_stolen_mask(). I don't have opinion on the new name. Perhaps kvm_is_protected_vm() is my preference though.
Paolo Bonzini
maintainerRe: [RFC PATCH v5 047/104] KVM: x86/mmu: add a private pointer to struct kvm_mmu_page
Quoted 8 lines · Kai Huang
Quoted 1 line
Do you mean below reply?
"I think use of
kvm_gfn_stolen_mask()should be minimized anyway. I would rename it to to kvm_{gfn,gpa}_private_mask and not return bool."I also mean we should not use
kvm_gfn_stolen_mask(). I don't have opinion on the new name. Perhapskvm_is_protected_vm()is my preference though.
But this is one of the case where it would survive, even with the changed name.
Paolo
Re: [RFC PATCH v5 047/104] KVM: x86/mmu: add a private pointer to struct kvm_mmu_page
Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · Kai Huang
Quoted 1 line
Do you mean below reply?
"I think use of
kvm_gfn_stolen_mask()should be minimized anyway. I would rename it to to kvm_{gfn,gpa}_private_mask and not return bool."
Quoted 8 lines
Quoted 2 lines
I also mean we should not use
kvm_gfn_stolen_mask(). I don't have opinion on the new name. Perhapskvm_is_protected_vm()is my preference though.But this is one of the case where it would survive, even with the changed name.
Paolo
Perhaps I confused you (sorry about that). Yes we do need the check here. I just dislike the function name.
Signature · 2 lines
Thanks, -Kai
RFC PATCH v5 48/104 KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Use private EPT to mirror Secure EPT, and On the change of the private EPT entry, invoke kvm_x86_ops hook in __handle_changed_spte() to propagate the change to Secure EPT.
On EPT violation, determine which EPT to use, private or shared, based on faulted GPA. When allocating an EPT page, record it (private or shared) in the page role. The private is passed down to the function as an argument as necessary. When the private EPT entry is changed, call the hook.
When zapping EPT, the EPT entry is frozen with the special EPT value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value. On populating the EPT entry, atomically set the entry.
For TDX, TDX SEAMCALL to update Secure EPT in addition to direct access to the private EPT entry. For the zapping case, freeing the EPT entry works. It can call TDX SEAMCALL in addition to TLB shootdown. For populating the private EPT entry, there can be a race condition without further protection
vcpu 1: populating 2M private EPT entry
vcpu 2: populating 4K private EPT entry
vcpu 2: TDX SEAMCALL to update 4K secure EPT => error vcpu 1: TDX SEAMCALL to update 4M secure EPT
To avoid the race, the frozen EPT entry is utilized. Instead of atomic update of the private EPT entry, freeze the entry, call the hook that invokes TDX SEAMCALL, set the entry to the final value (unfreeze).
Support 4K page only at this stage. 2M page support can be done in future patches.
--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 8 + arch/x86/kvm/mmu/mmu.c | 31 +++- arch/x86/kvm/mmu/tdp_iter.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 226 +++++++++++++++++++++++------ arch/x86/kvm/mmu/tdp_mmu.h | 13 +- virt/kvm/kvm_main.c | 1 + 7 files changed, 232 insertions(+), 51 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -0
@@ -91,6 +91,8 @@ KVM_X86_OP(set_tss_addr) KVM_X86_OP(set_identity_map_addr) KVM_X86_OP(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP(free_private_sp) +KVM_X86_OP(handle_changed_private_spte) KVM_X86_OP_NULL(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)
arch/x86/include/asm/kvm_host.h +8 -0
@@ -433,6 +433,7 @@ struct kvm_mmu { struct kvm_mmu_page *sp);void (*invlpg)(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa);hpa_t root_hpa; +hpa_t private_root_hpa;gpa_t root_pgd;union kvm_mmu_role mmu_role;u8 root_level;
@@ -1433,6 +1434,13 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*free_private_sp)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_sp); +void (*handle_changed_private_spte)( +struct kvm *kvm,gfn_t gfn,enum pg_level level, +kvm_pfn_t old_pfn,bool was_present,bool was_leaf, +kvm_pfn_t new_pfn,bool is_present,bool is_leaf,void *sept_page); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);
arch/x86/kvm/mmu/mmu.c +29 -2
@@ -3422,6 +3422,7 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) {struct kvm_mmu *mmu = vcpu->arch.mmu;u8 shadow_root_level = mmu->shadow_root_level; +gfn_t gfn_stolen =kvm_gfn_stolen_mask (vcpu->kvm);hpa_t root;unsigned i;int r;
@@ -3432,7 +3433,11 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (is_tdp_mmu_enabled (vcpu->kvm)) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (gfn_stolen && !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root; + } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root_hpa = root; }else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level, true);
@@ -5596,6 +5601,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)int i; mmu->root_hpa =INVALID_PAGE ; + mmu->private_root_hpa =INVALID_PAGE ; mmu->root_pgd =0 ;for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ;
@@ -5772,6 +5778,10 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)write_unlock (&kvm->mmu_lock); +/* +* For now private root is never invalidate during VM is running, +* so this can only happen for shared roots. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock);kvm_tdp_mmu_zap_invalidated_roots (kvm);
@@ -5871,7 +5881,8 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)if (is_tdp_mmu_enabled (kvm)) {for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) flush =kvm_tdp_mmu_zap_gfn_range (kvm, i, gfn_start, - gfn_end, flush); + gfn_end, flush, + false); }if (flush)
@@ -5904,6 +5915,11 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* For now this can only happen for non-TD VM, because TD private +* mapping doesn't support write protection. kvm_tdp_mmu_wrprot_slot() +* will give a WARN() if it hits for TD. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock); flush |=kvm_tdp_mmu_wrprot_slot (kvm, memslot, start_level);
@@ -5952,6 +5968,9 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, sp =sptep_to_sp (sptep); pfn =spte_to_pfn (*sptep); +/* Private page dirty logging is not supported. */ +KVM_BUG_ON (is_private_spte (sptep), kvm); +/* * We cannot do huge page mapping for indirect shadow pages, * which are found on the last rmap (level = 1) when not using
@@ -5992,6 +6011,11 @@ void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* This should only be reachable in case of log-dirty, wihch TD private +* mapping doesn't support so far. kvm_tdp_mmu_zap_collapsible_sptes() +* internally gives a WARN() when it hits. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock);kvm_tdp_mmu_zap_collapsible_sptes (kvm, slot);
@@ -6266,6 +6290,9 @@ int kvm_mmu_module_init(void)void kvm_mmu_destroy (struct kvm_vcpu *vcpu) {kvm_mmu_unload (vcpu); +if (is_tdp_mmu_enabled (vcpu->kvm)) +mmu_free_root_page (vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, +NULL );free_mmu_pages (&vcpu->arch.root_mmu);free_mmu_pages (&vcpu->arch.guest_mmu);mmu_free_memory_caches (vcpu);
arch/x86/kvm/mmu/tdp_iter.h +1 -1
@@ -28,7 +28,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (stolen bits included) mapped by the current SPTE */ gfn_t gfn;/* The level of the root page given to the iterator */ int root_level;
arch/x86/kvm/mmu/tdp_mmu.c +183 -43
467 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.h +8 -5
@@ -5,7 +5,7 @@#include <linux/kvm_host.h> -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu); +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private);__must_check static inline bool kvm_tdp_mmu_get_root (struct kvm *kvm,struct kvm_mmu_page *root)
@@ -20,11 +20,14 @@ void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root, bool shared);bool __kvm_tdp_mmu_zap_gfn_range (struct kvm *kvm,int as_id,gfn_t start, -gfn_t end,bool can_yield,bool flush); +gfn_t end,bool can_yield,bool flush, +bool zap_private);static inline bool kvm_tdp_mmu_zap_gfn_range (struct kvm *kvm,int as_id, -gfn_t start,gfn_t end,bool flush) +gfn_t start,gfn_t end,bool flush, +bool zap_private) { -return __kvm_tdp_mmu_zap_gfn_range (kvm, as_id, start, end, true, flush); +return __kvm_tdp_mmu_zap_gfn_range (kvm, as_id, start, end, true, flush, + zap_private); }static inline bool kvm_tdp_mmu_zap_sp (struct kvm *kvm,struct kvm_mmu_page *sp) {
@@ -41,7 +44,7 @@ static inline bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)*/ lockdep_assert_held_write (&kvm->mmu_lock);return __kvm_tdp_mmu_zap_gfn_range (kvm,kvm_mmu_page_as_id (sp), - sp->gfn, end, false, false); + sp->gfn, end, false, false, false); }void kvm_tdp_mmu_zap_all (struct kvm *kvm);
virt/kvm/kvm_main.c +1 -0
@@ -190,6 +190,7 @@ bool kvm_is_reserved_pfn(kvm_pfn_t pfn)return true; } +EXPORT_SYMBOL_GPL (kvm_is_reserved_pfn);/* * Switches to specified vcpu, until a matching vcpu_put()
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 048/104] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
Quoted 3 lines · On Fri, 2022-03-04 at 11:49 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Use private EPT to mirror Secure EPT, and On the change of the private EPT
"on the change".
Quoted 18 lines
entry, invoke
kvm_x86_opshook in__handle_changed_spte()to propagate the change to Secure EPT.On EPT violation, determine which EPT to use, private or shared, based on faulted GPA. When allocating an EPT page, record it (private or shared) in the page role. The private is passed down to the function as an argument as necessary. When the private EPT entry is changed, call the hook.
When zapping EPT, the EPT entry is frozen with the special EPT value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value. On populating the EPT entry, atomically set the entry.
For TDX, TDX SEAMCALL to update Secure EPT in addition to direct access to the private EPT entry. For the zapping case, freeing the EPT entry works. It can call TDX SEAMCALL in addition to TLB shootdown. For populating the private EPT entry, there can be a race condition without further protection
Quoted 4 lines
vcpu 1: populating 2M private EPT entry
vcpu 2: populating 4K private EPT entry
vcpu 2: TDX SEAMCALL to update 4K secure EPT => error vcpu 1: TDX SEAMCALL to update 4M secure EPT
2M secure EPT
Quoted 7 lines
To avoid the race, the frozen EPT entry is utilized. Instead of atomic update of the private EPT entry, freeze the entry, call the hook that invokes TDX SEAMCALL, set the entry to the final value (unfreeze).
Support 4K page only at this stage. 2M page support can be done in future patches.
Btw, I'd like to see this as a patch to handle schematic of private mapping in MMU code, and put this patch close to other infrastructure patches such as "stolen GPA infrastructure" and "private_sp", so people can get a clear view on what does the schematic of "private mapping" meaning and how to handle it before jumping to TDX details.
In this patch, you have a "mirrored private page table", this is an important concept and please explain it in commit message. Only with this, your above race condition case makes sense.
Also, you mentioned you will record private or shared in page role. It seems I don't see it. Anyway, you also have SPTE_PRIVATE_PROHIBIT. It's not entirely clearly to me why you need it, or what's advantage between using page.role vs it. Please put those patches together so we can have a clear understanding on your decisions.
IMHO, in general please reorganize the MMU related patches into below way:
1) Patches to introduce schematic of private mapping, and how to handle. You can use TDX as background to explain, but IMHO please use common name such as "private page table" instead of "Secure EPT". Those patches can include:
- concept of protected VM, private/shared mapping (current "GPA stolen bits infrastructure" patch).
- shadow_nonpresent_mask to support setting "Suppress #VE" bit.
- per-VM MMIO value/mask
- 'kvm_mmu_page->private_sp'
- mirrored private page table (this patch).
- SPTE_PRIVATE_PROHIBIT (vs role.private, etc)
- Anything else?
2) After you clearly explain the schematic of private mapping, you can declare some features cannot work with private mapping, such as log-dirty, fast page fault, so that you can disable them in separate patches
3) TDX specific handling.
Order of 2) and 3) doesn't matter, of course.
And when introduce a patch, please make sure it doesn't break existing things, even logically. While I understand we want to split out independent small logic as small patches (as preparation to support main patches), please don't break anything in one patch.
My 2cents above.
Quoted 718 lines
Co-developed-by Kai Huang <[email protected]>Signed-off-by Kai Huang <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 8 + arch/x86/kvm/mmu/mmu.c | 31 +++- arch/x86/kvm/mmu/tdp_iter.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 226 +++++++++++++++++++++++------ arch/x86/kvm/mmu/tdp_mmu.h | 13 +- virt/kvm/kvm_main.c | 1 + 7 files changed, 232 insertions(+), 51 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +2
-0@@ -91,6 +91,8 @@ KVM_X86_OP(set_tss_addr) KVM_X86_OP(set_identity_map_addr) KVM_X86_OP(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP(free_private_sp) +KVM_X86_OP(handle_changed_private_spte) KVM_X86_OP_NULL(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)arch/x86/include/asm/kvm_host.h +8
-0@@ -433,6 +433,7 @@ struct kvm_mmu { struct kvm_mmu_page *sp);void (*invlpg)(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa);hpa_t root_hpa; +hpa_t private_root_hpa;gpa_t root_pgd;union kvm_mmu_role mmu_role;u8 root_level;@@ -1433,6 +1434,13 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*free_private_sp)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_sp); +void (*handle_changed_private_spte)( +struct kvm *kvm,gfn_t gfn,enum pg_level level, +kvm_pfn_t old_pfn,bool was_present,bool was_leaf, +kvm_pfn_t new_pfn,bool is_present,bool is_leaf,void *sept_page); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);arch/x86/kvm/mmu/mmu.c +29
-2@@ -3422,6 +3422,7 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) {struct kvm_mmu *mmu = vcpu->arch.mmu;u8 shadow_root_level = mmu->shadow_root_level; +gfn_t gfn_stolen =kvm_gfn_stolen_mask (vcpu->kvm);hpa_t root;unsigned i;int r;@@ -3432,7 +3433,11 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (is_tdp_mmu_enabled (vcpu->kvm)) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (gfn_stolen && !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root; + } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root_hpa = root; }else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level, true);@@ -5596,6 +5601,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)int i; mmu->root_hpa =INVALID_PAGE ; + mmu->private_root_hpa =INVALID_PAGE ; mmu->root_pgd =0 ;for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ;@@ -5772,6 +5778,10 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)write_unlock (&kvm->mmu_lock); +/* +* For now private root is never invalidate during VM is running, +* so this can only happen for shared roots. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock);kvm_tdp_mmu_zap_invalidated_roots (kvm);@@ -5871,7 +5881,8 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)if (is_tdp_mmu_enabled (kvm)) {for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) flush =kvm_tdp_mmu_zap_gfn_range (kvm, i, gfn_start, - gfn_end, flush); + gfn_end, flush, + false); }if (flush)@@ -5904,6 +5915,11 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* For now this can only happen for non-TD VM, because TD private +* mapping doesn't support write protection. kvm_tdp_mmu_wrprot_slot() +* will give a WARN() if it hits for TD. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock); flush |=kvm_tdp_mmu_wrprot_slot (kvm, memslot, start_level);@@ -5952,6 +5968,9 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, sp =sptep_to_sp (sptep); pfn =spte_to_pfn (*sptep); +/* Private page dirty logging is not supported. */ +KVM_BUG_ON (is_private_spte (sptep), kvm); +/* * We cannot do huge page mapping for indirect shadow pages, * which are found on the last rmap (level = 1) when not using @@ -5992,6 +6011,11 @@ void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* This should only be reachable in case of log-dirty, wihch TD private +* mapping doesn't support so far. kvm_tdp_mmu_zap_collapsible_sptes() +* internally gives a WARN() when it hits. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock);kvm_tdp_mmu_zap_collapsible_sptes (kvm, slot);@@ -6266,6 +6290,9 @@ int kvm_mmu_module_init(void)void kvm_mmu_destroy (struct kvm_vcpu *vcpu) {kvm_mmu_unload (vcpu); +if (is_tdp_mmu_enabled (vcpu->kvm)) +mmu_free_root_page (vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, +NULL );free_mmu_pages (&vcpu->arch.root_mmu);free_mmu_pages (&vcpu->arch.guest_mmu);mmu_free_memory_caches (vcpu);arch/x86/kvm/mmu/tdp_iter.h +1
-1@@ -28,7 +28,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (stolen bits included) mapped by the current SPTE */ gfn_t gfn;/* The level of the root page given to the iterator */ int root_level;arch/x86/kvm/mmu/tdp_mmu.c +183
-43@@ -53,6 +53,11 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)rcu_barrier (); } +static gfn_t tdp_iter_gfn_unalias (struct kvm *kvm,struct tdp_iter *iter) +{ +return kvm_gfn_unalias (kvm, iter->gfn); +} +static bool zap_gfn_range (struct kvm *kvm,struct kvm_mmu_page *root,gfn_t start,gfn_t end,bool can_yield,bool flush,bool shared);@@ -175,10 +180,13 @@ static union kvm_mmu_page_role page_role_for_level(struct kvm_vcpu *vcpu, }static struct kvm_mmu_page *alloc_tdp_mmu_page (struct kvm_vcpu *vcpu,gfn_t gfn, -int level) +int level,bool private) {struct kvm_mmu_page *sp; +WARN_ON (level != vcpu->arch.mmu->shadow_root_level && +kvm_is_private_gfn (vcpu->kvm, gfn) != private); +WARN_ON (level == vcpu->arch.mmu->shadow_root_level && gfn !=0 ); sp =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_page_header_cache); sp->spt =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_shadow_page_cache);set_page_private (virt_to_page (sp->spt), (unsigned long )sp);@@ -186,14 +194,19 @@ static struct kvm_mmu_page *alloc_tdp_mmu_page(struct kvm_vcpu *vcpu, gfn_t gfn, sp->role.word =page_role_for_level (vcpu, level).word; sp->gfn = gfn; sp->tdp_mmu_page = true; -kvm_mmu_init_private_sp (sp); + +if (private) +kvm_mmu_alloc_private_sp (vcpu, sp); +else +kvm_mmu_init_private_sp (sp);trace_kvm_mmu_get_page (sp, true);return sp; } -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu) +static struct kvm_mmu_page *kvm_tdp_mmu_get_vcpu_root (struct kvm_vcpu *vcpu, +bool private) {union kvm_mmu_page_role role;struct kvm *kvm = vcpu->kvm;@@ -206,11 +219,13 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)/* Check for an existing root before allocating a new one. */ for_each_tdp_mmu_root (kvm, root,kvm_mmu_role_as_id (role)) {if (root->role.word == role.word && +is_private_sp (root) == private &&kvm_tdp_mmu_get_root (kvm, root)) goto out; } - root =alloc_tdp_mmu_page (vcpu,0 , vcpu->arch.mmu->shadow_root_level); + root =alloc_tdp_mmu_page (vcpu,0 , vcpu->arch.mmu->shadow_root_level, + private);refcount_set (&root->tdp_mmu_root_count,1 );spin_lock (&kvm->arch.tdp_mmu_pages_lock);@@ -218,12 +233,17 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)spin_unlock (&kvm->arch.tdp_mmu_pages_lock); out: -return __pa (root->spt); +return root; +} + +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private) +{ +return __pa (kvm_tdp_mmu_get_vcpu_root (vcpu, private)->spt); }static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared); +bool private_spte,u64 old_spte, +u64 new_spte,int level,bool shared);static void handle_changed_spte_acc_track (u64 old_spte,u64 new_spte,int level) {@@ -321,6 +341,7 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,int level = sp->role.level;gfn_t base_gfn = sp->gfn;int i; +bool private_sp =is_private_sp (sp);trace_kvm_mmu_prepare_zap_page (sp);@@ -370,7 +391,7 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,*/ WRITE_ONCE (*sptep,SHADOW_REMOVED_SPTE ); } -handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, +handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, private_sp, old_child_spte,SHADOW_REMOVED_SPTE , level, shared); }@@ -378,6 +399,17 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,kvm_flush_remote_tlbs_with_address (kvm, base_gfn,KVM_PAGES_PER_HPAGE (level +1 )); +if (private_sp && +WARN_ON (static_call (kvm_x86_free_private_sp)( + kvm, sp->gfn, sp->role.level, +kvm_mmu_private_sp (sp)))) { +/* +* Failed to unlink Secure EPT page and there is nothing to do +* further. Intentionally leak the page to prevent the kernel +* from accessing the encrypted page. +*/ +kvm_mmu_init_private_sp (sp); + }call_rcu (&sp->rcu_head, tdp_mmu_free_sp_rcu_callback); }@@ -386,6 +418,7 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,* @kvm: kvm instance * @as_id: the address space of the paging structure the SPTE was a part of * @gfn: the base GFN that was mapped by the SPTE +* @private_spte: the SPTE is private or not * @old_spte: The value of the SPTE before the change * @new_spte: The value of the SPTE after the change * @level: the level of the PT the SPTE is part of in the paging structure @@ -397,14 +430,16 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,* This function must be called for all TDP SPTE modifications. */ static void __handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared) +bool private_spte,u64 old_spte, +u64 new_spte,int level,bool shared) {bool was_present =is_shadow_present_pte (old_spte);bool is_present =is_shadow_present_pte (new_spte);bool was_leaf = was_present &&is_last_spte (old_spte, level);bool is_leaf = is_present &&is_last_spte (new_spte, level); -bool pfn_changed =spte_to_pfn (old_spte) !=spte_to_pfn (new_spte); +kvm_pfn_t old_pfn =spte_to_pfn (old_spte); +kvm_pfn_t new_pfn =spte_to_pfn (new_spte); +bool pfn_changed = old_pfn != new_pfn;WARN_ON (level >PT64_ROOT_MAX_LEVEL );WARN_ON (level <PG_LEVEL_4K );@@ -468,23 +503,49 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,if (was_leaf &&is_dirty_spte (old_spte) && (!is_present || !is_dirty_spte (new_spte) || pfn_changed)) -kvm_set_pfn_dirty (spte_to_pfn (old_spte)); +kvm_set_pfn_dirty (old_pfn); + +/* +* Special handling for the private mapping. We are either +* setting up new mapping at middle level page table, or leaf, +* or tearing down existing mapping. +*/ +if (private_spte) { +void *sept_page =NULL ; + +if (is_present && !is_leaf) { +struct kvm_mmu_page *sp =to_shadow_page (pfn_to_hpa (new_pfn)); + + sept_page =kvm_mmu_private_sp (sp); +WARN_ON (!sept_page); +WARN_ON (sp->role.level +1 != level); +WARN_ON (sp->gfn != gfn); + } + +static_call (kvm_x86_handle_changed_private_spte)( + kvm, gfn, level, + old_pfn, was_present, was_leaf, + new_pfn, is_present, is_leaf, sept_page); + }/* * Recursively handle child PTs if the change removed a subtree from * the paging structure. */ -if (was_present && !was_leaf && (pfn_changed || !is_present)) +if (was_present && !was_leaf && (pfn_changed || !is_present)) { +WARN_ON (private_spte != +is_private_spte (spte_to_child_pt (old_spte, level)));handle_removed_tdp_mmu_page (kvm,spte_to_child_pt (old_spte, level), shared); + } }static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared) +bool private_spte,u64 old_spte,u64 new_spte, +int level,bool shared) { -__handle_changed_spte (kvm, as_id, gfn, old_spte, new_spte, level, - shared); +__handle_changed_spte (kvm, as_id, gfn, private_spte, + old_spte, new_spte, level, shared);handle_changed_spte_acc_track (old_spte, new_spte, level);handle_changed_spte_dirty_log (kvm, as_id, gfn, old_spte, new_spte, level);@@ -505,6 +566,10 @@ static inline bool tdp_mmu_set_spte_atomic(struct kvm *kvm,struct tdp_iter *iter, u64 new_spte) { + bool freeze_spte = is_private_spte(iter->sptep) && + !is_removed_spte (new_spte); +u64 tmp_spte = freeze_spte ?SHADOW_REMOVED_SPTE : new_spte; +WARN_ON_ONCE (iter->yielded);lockdep_assert_held_read (&kvm->mmu_lock);@@ -521,13 +586,16 @@ static inline bool tdp_mmu_set_spte_atomic(struct kvm *kvm,* does not hold the mmu_lock. */ if (cmpxchg64 (rcu_dereference (iter->sptep), iter->old_spte, - new_spte) != iter->old_spte) + tmp_spte) != iter->old_spte)return false; -__handle_changed_spte (kvm, iter->as_id, iter->gfn, iter->old_spte, - new_spte, iter->level, true); +__handle_changed_spte (kvm, iter->as_id, iter->gfn,is_private_spte (iter->sptep), + iter->old_spte, new_spte, iter->level, true);handle_changed_spte_acc_track (iter->old_spte, new_spte, iter->level); +if (freeze_spte) +WRITE_ONCE (*rcu_dereference (iter->sptep), new_spte); +return true; }@@ -603,8 +671,8 @@ static inline void __tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter,WRITE_ONCE (*rcu_dereference (iter->sptep), new_spte); -__handle_changed_spte (kvm, iter->as_id, iter->gfn, iter->old_spte, - new_spte, iter->level, false); +__handle_changed_spte (kvm, iter->as_id, iter->gfn,is_private_spte (iter->sptep), + iter->old_spte, new_spte, iter->level, false);if (record_acc_track)handle_changed_spte_acc_track (iter->old_spte, new_spte, iter->level);@@ -644,9 +712,10 @@ static inline void tdp_mmu_set_spte_no_dirty_log(struct kvm *kvm,continue ; \ else -#define tdp_mmu_for_each_pte (_iter, _mmu, _start, _end) \ - for_each_tdp_pte(_iter, __va(_mmu->root_hpa), \ - _mmu->shadow_root_level, _start, _end) +#define tdp_mmu_for_each_pte (_iter, _mmu, _private, _start, _end) \ + for_each_tdp_pte(_iter, \ + __va((_private) ? _mmu->private_root_hpa : _mmu->root_hpa), \ + _mmu->shadow_root_level, _start, _end)/* * Yield if the MMU lock is contended or this thread needs to return control @@ -731,6 +800,18 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,*/ end =min (end, max_gfn_host); +/* +* Extend [start, end) to include GFN shared bit when TDX is enabled, +* and for shared mapping range. +*/ +if (is_private_sp (root)) { + start =kvm_gfn_private (kvm, start); + end =kvm_gfn_private (kvm, end); + }else { + start =kvm_gfn_shared (kvm, start); + end =kvm_gfn_shared (kvm, end); + } +kvm_lockdep_assert_mmu_lock_held (kvm, shared);rcu_read_lock ();@@ -783,13 +864,18 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,* MMU lock. */ bool __kvm_tdp_mmu_zap_gfn_range (struct kvm *kvm,int as_id,gfn_t start, -gfn_t end,bool can_yield,bool flush) +gfn_t end,bool can_yield,bool flush, +bool zap_private) {struct kvm_mmu_page *root; -for_each_tdp_mmu_root_yield_safe (kvm, root, as_id, false) +for_each_tdp_mmu_root_yield_safe (kvm, root, as_id, false) { +/* Skip private page table if not requested */ +if (!zap_private &&is_private_sp (root)) +continue ; flush =zap_gfn_range (kvm, root, start, end, can_yield, flush, false); + }return flush; }@@ -800,7 +886,7 @@ void kvm_tdp_mmu_zap_all(struct kvm *kvm)int i;for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) - flush =kvm_tdp_mmu_zap_gfn_range (kvm, i,0 ,-1ull , flush); + flush =kvm_tdp_mmu_zap_gfn_range (kvm, i,0 ,-1ull , flush, true);if (flush)kvm_flush_remote_tlbs (kvm);@@ -851,6 +937,13 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm)while (root) { next_root =next_invalidated_root (kvm, root); +/* +* Private table is only torn down when VM is destroyed. +* It is a bug to zap private table here. +*/ +if (WARN_ON (is_private_sp (root))) + goto out; +rcu_read_unlock (); flush =zap_gfn_range (kvm, root,0 ,-1ull , true, flush, true);@@ -865,7 +958,7 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm)rcu_read_lock (); } - +out:rcu_read_unlock ();if (flush)@@ -897,9 +990,16 @@ void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm)struct kvm_mmu_page *root;lockdep_assert_held_write (&kvm->mmu_lock); -list_for_each_entry (root, &kvm->arch.tdp_mmu_roots, link) +list_for_each_entry (root, &kvm->arch.tdp_mmu_roots, link) { +/* +* Skip private root since private page table +* is only torn down when VM is destroyed. +*/ +if (is_private_sp (root)) +continue ;if (refcount_inc_not_zero (&root->tdp_mmu_root_count)) root->role.invalid = true; + } }/* @@ -914,14 +1014,23 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,u64 new_spte;int ret =RET_PF_FIXED ;bool wrprot = false; +unsigned long pte_access =ACC_ALL ;WARN_ON (sp->role.level != fault->goal_level); + +/* TDX shared GPAs are no executable, enforce this for the SDV. */ +if (!kvm_is_private_gfn (vcpu->kvm, iter->gfn)) + pte_access &= ~ACC_EXEC_MASK ; +if (unlikely (!fault->slot)) - new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL ); + new_spte =make_mmio_spte (vcpu, +tdp_iter_gfn_unalias (vcpu->kvm, iter), + pte_access);else - wrprot =make_spte (vcpu, sp, fault->slot,ACC_ALL , iter->gfn, - fault->pfn, iter->old_spte, fault->prefetch, true, - fault->map_writable, &new_spte); + wrprot =make_spte (vcpu, sp, fault->slot, pte_access, +tdp_iter_gfn_unalias (vcpu->kvm, iter), + fault->pfn, iter->old_spte, fault->prefetch, + true, fault->map_writable, &new_spte);if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;@@ -959,7 +1068,8 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, }static bool tdp_mmu_populate_nonleaf ( -struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool account_nx) +struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool is_private, +bool account_nx) {struct kvm_mmu_page *sp;u64 *child_pt;@@ -968,7 +1078,7 @@ static bool tdp_mmu_populate_nonleaf(WARN_ON (is_shadow_present_pte (iter->old_spte));WARN_ON (is_removed_spte (iter->old_spte)); - sp =alloc_tdp_mmu_page (vcpu, iter->gfn, iter->level -1 ); + sp =alloc_tdp_mmu_page (vcpu, iter->gfn, iter->level -1 , is_private); child_pt = sp->spt; new_spte =make_nonleaf_spte (child_pt, !shadow_accessed_mask);@@ -991,6 +1101,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) {struct kvm_mmu *mmu = vcpu->arch.mmu;struct tdp_iter iter; +gfn_t raw_gfn; +bool is_private;int ret;kvm_mmu_hugepage_adjust (vcpu, fault);@@ -999,7 +1111,17 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)rcu_read_lock (); -tdp_mmu_for_each_pte (iter, mmu, fault->gfn, fault->gfn +1 ) { + raw_gfn =gpa_to_gfn (fault->addr); + is_private =kvm_is_private_gfn (vcpu->kvm, raw_gfn); + +if (is_error_noslot_pfn (fault->pfn) ||kvm_is_reserved_pfn (fault->pfn)) { +if (is_private) { +rcu_read_unlock (); +return -EFAULT ; + } + } + +tdp_mmu_for_each_pte (iter, mmu, is_private, raw_gfn, raw_gfn +1 ) {if (fault->nx_huge_page_workaround_enabled)disallowed_hugepage_adjust (fault, iter.old_spte, iter.level);@@ -1015,6 +1137,12 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) is_large_pte(iter.old_spte)) {if (!tdp_mmu_zap_spte_atomic (vcpu->kvm, &iter))break ; +/* +* TODO: large page support. +* Doesn't support large page for TDX now +*/ +WARN_ON (is_private_spte (&iter.old_spte)); +/* * The iter must explicitly re-read the spte here @@ -1037,7 +1165,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) account_nx = fault->huge_page_disallowed && fault->req_level >= iter.level; -if (!tdp_mmu_populate_nonleaf (vcpu, &iter, account_nx)) +if (!tdp_mmu_populate_nonleaf ( + vcpu, &iter, is_private, account_nx))break ; } }@@ -1058,9 +1187,12 @@ bool kvm_tdp_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range, {struct kvm_mmu_page *root; -for_each_tdp_mmu_root_yield_safe (kvm, root, range->slot->as_id, false) +for_each_tdp_mmu_root_yield_safe (kvm, root, range->slot->as_id, false) { +if (is_private_sp (root)) +continue ; flush =zap_gfn_range (kvm, root, range->start, range->end, - range->may_block, flush, false); + range->may_block, flush, false); + }return flush; }@@ -1513,10 +1645,14 @@ int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes,struct kvm_mmu *mmu = vcpu->arch.mmu;gfn_t gfn = addr >>PAGE_SHIFT ;int leaf =-1 ; +bool is_private =kvm_is_private_gfn (vcpu->kvm, gfn); *root_level = vcpu->arch.mmu->shadow_root_level; -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +if (WARN_ON (is_private)) +return leaf; + +tdp_mmu_for_each_pte (iter, mmu, false, gfn, gfn +1 ) { leaf = iter.level; sptes[leaf] = iter.old_spte; }@@ -1542,12 +1678,16 @@ u64 *kvm_tdp_mmu_fast_pf_get_last_sptep(struct kvm_vcpu *vcpu, u64 addr,struct kvm_mmu *mmu = vcpu->arch.mmu;gfn_t gfn = addr >>PAGE_SHIFT ;tdp_ptep_t sptep =NULL ; +bool is_private =kvm_is_private_gfn (vcpu->kvm, gfn); -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +if (is_private) + goto out; + +tdp_mmu_for_each_pte (iter, mmu, false, gfn, gfn +1 ) { *spte = iter.old_spte; sptep = iter.sptep; } - +out:/* * Perform the rcu_dereference to get the raw spte pointer value since * we are passing it up to fast_page_fault, which is shared with the arch/x86/kvm/mmu/tdp_mmu.h +8
-5@@ -5,7 +5,7 @@#include <linux/kvm_host.h> -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu); +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private);__must_check static inline bool kvm_tdp_mmu_get_root (struct kvm *kvm,struct kvm_mmu_page *root)@@ -20,11 +20,14 @@ void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root, bool shared);bool __kvm_tdp_mmu_zap_gfn_range (struct kvm *kvm,int as_id,gfn_t start, -gfn_t end,bool can_yield,bool flush); +gfn_t end,bool can_yield,bool flush, +bool zap_private);static inline bool kvm_tdp_mmu_zap_gfn_range (struct kvm *kvm,int as_id, -gfn_t start,gfn_t end,bool flush) +gfn_t start,gfn_t end,bool flush, +bool zap_private) { -return __kvm_tdp_mmu_zap_gfn_range (kvm, as_id, start, end, true, flush); +return __kvm_tdp_mmu_zap_gfn_range (kvm, as_id, start, end, true, flush, + zap_private); }static inline bool kvm_tdp_mmu_zap_sp (struct kvm *kvm,struct kvm_mmu_page *sp) {@@ -41,7 +44,7 @@ static inline bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)*/ lockdep_assert_held_write (&kvm->mmu_lock);return __kvm_tdp_mmu_zap_gfn_range (kvm,kvm_mmu_page_as_id (sp), - sp->gfn, end, false, false); + sp->gfn, end, false, false, false); }void kvm_tdp_mmu_zap_all (struct kvm *kvm);virt/kvm/kvm_main.c +1
-0@@ -190,6 +190,7 @@ bool kvm_is_reserved_pfn(kvm_pfn_t pfn)return true; } +EXPORT_SYMBOL_GPL (kvm_is_reserved_pfn);/* * Switches to specified vcpu, until a matching vcpu_put()
Re: [RFC PATCH v5 048/104] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
Quoted 610 lines · Kai Huang
Quoted 3 lines · On Fri, 2022-03-04 at 11:49 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Use private EPT to mirror Secure EPT, and On the change of the private EPT
"on the change".
Quoted 18 lines
entry, invoke
kvm_x86_opshook in__handle_changed_spte()to propagate the change to Secure EPT.On EPT violation, determine which EPT to use, private or shared, based on faulted GPA. When allocating an EPT page, record it (private or shared) in the page role. The private is passed down to the function as an argument as necessary. When the private EPT entry is changed, call the hook.
When zapping EPT, the EPT entry is frozen with the special EPT value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value. On populating the EPT entry, atomically set the entry.
For TDX, TDX SEAMCALL to update Secure EPT in addition to direct access to the private EPT entry. For the zapping case, freeing the EPT entry works. It can call TDX SEAMCALL in addition to TLB shootdown. For populating the private EPT entry, there can be a race condition without further protection
Quoted 4 lines
vcpu 1: populating 2M private EPT entry
vcpu 2: populating 4K private EPT entry
vcpu 2: TDX SEAMCALL to update 4K secure EPT => error vcpu 1: TDX SEAMCALL to update 4M secure EPT2M secure EPT
Quoted 7 lines
To avoid the race, the frozen EPT entry is utilized. Instead of atomic update of the private EPT entry, freeze the entry, call the hook that invokes TDX SEAMCALL, set the entry to the final value (unfreeze).
Support 4K page only at this stage. 2M page support can be done in future patches.
Btw, I'd like to see this as a patch to handle schematic of private mapping in MMU code, and put this patch close to other infrastructure patches such as "stolen GPA infrastructure" and "
private_sp", so people can get a clear view on what does the schematic of "private mapping" meaning and how to handle it before jumping to TDX details.In this patch, you have a "mirrored private page table", this is an important concept and please explain it in commit message. Only with this, your above race condition case makes sense.
Also, you mentioned you will record private or shared in page role. It seems I don't see it. Anyway, you also have
SPTE_PRIVATE_PROHIBIT. It's not entirely clearly to me why you need it, or what's advantage between using page.role vs it. Please put those patches together so we can have a clear understanding on your decisions.IMHO, in general please reorganize the MMU related patches into below way:
1) Patches to introduce schematic of private mapping, and how to handle. You can use TDX as background to explain, but IMHO please use common name such as "private page table" instead of "Secure EPT". Those patches can include:
- concept of protected VM, private/shared mapping (current "GPA stolen bits infrastructure" patch).
-shadow_nonpresent_maskto support setting "Suppress #VE" bit.
- per-VM MMIO value/mask
- 'kvm_mmu_page->private_sp'
- mirrored private page table (this patch).
-SPTE_PRIVATE_PROHIBIT(vs role.private, etc)
- Anything else?2) After you clearly explain the schematic of private mapping, you can declare some features cannot work with private mapping, such as log-dirty, fast page fault, so that you can disable them in separate patches
3) TDX specific handling.
Order of 2) and 3) doesn't matter, of course.
And when introduce a patch, please make sure it doesn't break existing things, even logically. While I understand we want to split out independent small logic as small patches (as preparation to support main patches), please don't break anything in one patch.
My 2cents above.
Quoted 524 lines
Co-developed-by Kai Huang <[email protected]>Signed-off-by Kai Huang <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 8 + arch/x86/kvm/mmu/mmu.c | 31 +++- arch/x86/kvm/mmu/tdp_iter.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 226 +++++++++++++++++++++++------ arch/x86/kvm/mmu/tdp_mmu.h | 13 +- virt/kvm/kvm_main.c | 1 + 7 files changed, 232 insertions(+), 51 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +2
-0@@ -91,6 +91,8 @@ KVM_X86_OP(set_tss_addr) KVM_X86_OP(set_identity_map_addr) KVM_X86_OP(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP(free_private_sp) +KVM_X86_OP(handle_changed_private_spte) KVM_X86_OP_NULL(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)arch/x86/include/asm/kvm_host.h +8
-0@@ -433,6 +433,7 @@ struct kvm_mmu { struct kvm_mmu_page *sp);void (*invlpg)(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa);hpa_t root_hpa; +hpa_t private_root_hpa;gpa_t root_pgd;union kvm_mmu_role mmu_role;u8 root_level;@@ -1433,6 +1434,13 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*free_private_sp)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_sp); +void (*handle_changed_private_spte)( +struct kvm *kvm,gfn_t gfn,enum pg_level level, +kvm_pfn_t old_pfn,bool was_present,bool was_leaf, +kvm_pfn_t new_pfn,bool is_present,bool is_leaf,void *sept_page); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);arch/x86/kvm/mmu/mmu.c +29
-2@@ -3422,6 +3422,7 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) {struct kvm_mmu *mmu = vcpu->arch.mmu;u8 shadow_root_level = mmu->shadow_root_level; +gfn_t gfn_stolen =kvm_gfn_stolen_mask (vcpu->kvm);hpa_t root;unsigned i;int r;@@ -3432,7 +3433,11 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (is_tdp_mmu_enabled (vcpu->kvm)) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (gfn_stolen && !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root; + } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root_hpa = root; }else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level, true);@@ -5596,6 +5601,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)int i; mmu->root_hpa =INVALID_PAGE ; + mmu->private_root_hpa =INVALID_PAGE ; mmu->root_pgd =0 ;for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ;@@ -5772,6 +5778,10 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)write_unlock (&kvm->mmu_lock); +/* +* For now private root is never invalidate during VM is running, +* so this can only happen for shared roots. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock);kvm_tdp_mmu_zap_invalidated_roots (kvm);@@ -5871,7 +5881,8 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)if (is_tdp_mmu_enabled (kvm)) {for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) flush =kvm_tdp_mmu_zap_gfn_range (kvm, i, gfn_start, - gfn_end, flush); + gfn_end, flush, + false); }if (flush)@@ -5904,6 +5915,11 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* For now this can only happen for non-TD VM, because TD private +* mapping doesn't support write protection. kvm_tdp_mmu_wrprot_slot() +* will give a WARN() if it hits for TD. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock); flush |=kvm_tdp_mmu_wrprot_slot (kvm, memslot, start_level);@@ -5952,6 +5968,9 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, sp =sptep_to_sp (sptep); pfn =spte_to_pfn (*sptep); +/* Private page dirty logging is not supported. */ +KVM_BUG_ON (is_private_spte (sptep), kvm); +/* * We cannot do huge page mapping for indirect shadow pages, * which are found on the last rmap (level = 1) when not using @@ -5992,6 +6011,11 @@ void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* This should only be reachable in case of log-dirty, wihch TD private +* mapping doesn't support so far. kvm_tdp_mmu_zap_collapsible_sptes() +* internally gives a WARN() when it hits. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock);kvm_tdp_mmu_zap_collapsible_sptes (kvm, slot);@@ -6266,6 +6290,9 @@ int kvm_mmu_module_init(void)void kvm_mmu_destroy (struct kvm_vcpu *vcpu) {kvm_mmu_unload (vcpu); +if (is_tdp_mmu_enabled (vcpu->kvm)) +mmu_free_root_page (vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, +NULL );free_mmu_pages (&vcpu->arch.root_mmu);free_mmu_pages (&vcpu->arch.guest_mmu);mmu_free_memory_caches (vcpu);arch/x86/kvm/mmu/tdp_iter.h +1
-1@@ -28,7 +28,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (stolen bits included) mapped by the current SPTE */ gfn_t gfn;/* The level of the root page given to the iterator */ int root_level;arch/x86/kvm/mmu/tdp_mmu.c +131
-30@@ -53,6 +53,11 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)rcu_barrier (); } +static gfn_t tdp_iter_gfn_unalias (struct kvm *kvm,struct tdp_iter *iter) +{ +return kvm_gfn_unalias (kvm, iter->gfn); +} +static bool zap_gfn_range (struct kvm *kvm,struct kvm_mmu_page *root,gfn_t start,gfn_t end,bool can_yield,bool flush,bool shared);@@ -175,10 +180,13 @@ static union kvm_mmu_page_role page_role_for_level(struct kvm_vcpu *vcpu, }static struct kvm_mmu_page *alloc_tdp_mmu_page (struct kvm_vcpu *vcpu,gfn_t gfn, -int level) +int level,bool private) {struct kvm_mmu_page *sp; +WARN_ON (level != vcpu->arch.mmu->shadow_root_level && +kvm_is_private_gfn (vcpu->kvm, gfn) != private); +WARN_ON (level == vcpu->arch.mmu->shadow_root_level && gfn !=0 ); sp =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_page_header_cache); sp->spt =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_shadow_page_cache);set_page_private (virt_to_page (sp->spt), (unsigned long )sp);@@ -186,14 +194,19 @@ static struct kvm_mmu_page *alloc_tdp_mmu_page(struct kvm_vcpu *vcpu, gfn_t gfn, sp->role.word =page_role_for_level (vcpu, level).word; sp->gfn = gfn; sp->tdp_mmu_page = true; -kvm_mmu_init_private_sp (sp); + +if (private) +kvm_mmu_alloc_private_sp (vcpu, sp); +else +kvm_mmu_init_private_sp (sp);trace_kvm_mmu_get_page (sp, true);return sp; } -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu) +static struct kvm_mmu_page *kvm_tdp_mmu_get_vcpu_root (struct kvm_vcpu *vcpu, +bool private) {union kvm_mmu_page_role role;struct kvm *kvm = vcpu->kvm;@@ -206,11 +219,13 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)/* Check for an existing root before allocating a new one. */ for_each_tdp_mmu_root (kvm, root,kvm_mmu_role_as_id (role)) {if (root->role.word == role.word && +is_private_sp (root) == private &&kvm_tdp_mmu_get_root (kvm, root)) goto out; } - root =alloc_tdp_mmu_page (vcpu,0 , vcpu->arch.mmu->shadow_root_level); + root =alloc_tdp_mmu_page (vcpu,0 , vcpu->arch.mmu->shadow_root_level, + private);refcount_set (&root->tdp_mmu_root_count,1 );spin_lock (&kvm->arch.tdp_mmu_pages_lock);@@ -218,12 +233,17 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)spin_unlock (&kvm->arch.tdp_mmu_pages_lock); out: -return __pa (root->spt); +return root; +} + +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private) +{ +return __pa (kvm_tdp_mmu_get_vcpu_root (vcpu, private)->spt); }static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared); +bool private_spte,u64 old_spte, +u64 new_spte,int level,bool shared);static void handle_changed_spte_acc_track (u64 old_spte,u64 new_spte,int level) {@@ -321,6 +341,7 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,int level = sp->role.level;gfn_t base_gfn = sp->gfn;int i; +bool private_sp =is_private_sp (sp);trace_kvm_mmu_prepare_zap_page (sp);@@ -370,7 +391,7 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,*/ WRITE_ONCE (*sptep,SHADOW_REMOVED_SPTE ); } -handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, +handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, private_sp, old_child_spte,SHADOW_REMOVED_SPTE , level, shared); }@@ -378,6 +399,17 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,kvm_flush_remote_tlbs_with_address (kvm, base_gfn,KVM_PAGES_PER_HPAGE (level +1 )); +if (private_sp && +WARN_ON (static_call (kvm_x86_free_private_sp)( + kvm, sp->gfn, sp->role.level, +kvm_mmu_private_sp (sp)))) { +/* +* Failed to unlink Secure EPT page and there is nothing to do +* further. Intentionally leak the page to prevent the kernel +* from accessing the encrypted page. +*/ +kvm_mmu_init_private_sp (sp); + }call_rcu (&sp->rcu_head, tdp_mmu_free_sp_rcu_callback); }@@ -386,6 +418,7 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,* @kvm: kvm instance * @as_id: the address space of the paging structure the SPTE was a part of * @gfn: the base GFN that was mapped by the SPTE +* @private_spte: the SPTE is private or not * @old_spte: The value of the SPTE before the change * @new_spte: The value of the SPTE after the change * @level: the level of the PT the SPTE is part of in the paging structure @@ -397,14 +430,16 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,* This function must be called for all TDP SPTE modifications. */ static void __handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared) +bool private_spte,u64 old_spte, +u64 new_spte,int level,bool shared) {bool was_present =is_shadow_present_pte (old_spte);bool is_present =is_shadow_present_pte (new_spte);bool was_leaf = was_present &&is_last_spte (old_spte, level);bool is_leaf = is_present &&is_last_spte (new_spte, level); -bool pfn_changed =spte_to_pfn (old_spte) !=spte_to_pfn (new_spte); +kvm_pfn_t old_pfn =spte_to_pfn (old_spte); +kvm_pfn_t new_pfn =spte_to_pfn (new_spte); +bool pfn_changed = old_pfn != new_pfn;WARN_ON (level >PT64_ROOT_MAX_LEVEL );WARN_ON (level <PG_LEVEL_4K );@@ -468,23 +503,49 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,if (was_leaf &&is_dirty_spte (old_spte) && (!is_present || !is_dirty_spte (new_spte) || pfn_changed)) -kvm_set_pfn_dirty (spte_to_pfn (old_spte)); +kvm_set_pfn_dirty (old_pfn); + +/* +* Special handling for the private mapping. We are either +* setting up new mapping at middle level page table, or leaf, +* or tearing down existing mapping. +*/ +if (private_spte) { +void *sept_page =NULL ; + +if (is_present && !is_leaf) { +struct kvm_mmu_page *sp =to_shadow_page (pfn_to_hpa (new_pfn)); + + sept_page =kvm_mmu_private_sp (sp); +WARN_ON (!sept_page); +WARN_ON (sp->role.level +1 != level); +WARN_ON (sp->gfn != gfn); + } + +static_call (kvm_x86_handle_changed_private_spte)( + kvm, gfn, level, + old_pfn, was_present, was_leaf, + new_pfn, is_present, is_leaf, sept_page); + }/* * Recursively handle child PTs if the change removed a subtree from * the paging structure. */ -if (was_present && !was_leaf && (pfn_changed || !is_present)) +if (was_present && !was_leaf && (pfn_changed || !is_present)) { +WARN_ON (private_spte != +is_private_spte (spte_to_child_pt (old_spte, level)));handle_removed_tdp_mmu_page (kvm,spte_to_child_pt (old_spte, level), shared); + } }static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared) +bool private_spte,u64 old_spte,u64 new_spte, +int level,bool shared) { -__handle_changed_spte (kvm, as_id, gfn, old_spte, new_spte, level, - shared); +__handle_changed_spte (kvm, as_id, gfn, private_spte, + old_spte, new_spte, level, shared);handle_changed_spte_acc_track (old_spte, new_spte, level);handle_changed_spte_dirty_log (kvm, as_id, gfn, old_spte, new_spte, level);@@ -505,6 +566,10 @@ static inline bool tdp_mmu_set_spte_atomic(struct kvm *kvm,struct tdp_iter *iter, u64 new_spte) { + bool freeze_spte = is_private_spte(iter->sptep) && + !is_removed_spte (new_spte); +u64 tmp_spte = freeze_spte ?SHADOW_REMOVED_SPTE : new_spte; +WARN_ON_ONCE (iter->yielded);lockdep_assert_held_read (&kvm->mmu_lock);@@ -521,13 +586,16 @@ static inline bool tdp_mmu_set_spte_atomic(struct kvm *kvm,* does not hold the mmu_lock. */ if (cmpxchg64 (rcu_dereference (iter->sptep), iter->old_spte, - new_spte) != iter->old_spte) + tmp_spte) != iter->old_spte)return false; -__handle_changed_spte (kvm, iter->as_id, iter->gfn, iter->old_spte, - new_spte, iter->level, true); +__handle_changed_spte (kvm, iter->as_id, iter->gfn,is_private_spte (iter->sptep), + iter->old_spte, new_spte, iter->level, true);handle_changed_spte_acc_track (iter->old_spte, new_spte, iter->level); +if (freeze_spte) +WRITE_ONCE (*rcu_dereference (iter->sptep), new_spte); +return true; }@@ -603,8 +671,8 @@ static inline void __tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter,WRITE_ONCE (*rcu_dereference (iter->sptep), new_spte); -__handle_changed_spte (kvm, iter->as_id, iter->gfn, iter->old_spte, - new_spte, iter->level, false); +__handle_changed_spte (kvm, iter->as_id, iter->gfn,is_private_spte (iter->sptep), + iter->old_spte, new_spte, iter->level, false);if (record_acc_track)handle_changed_spte_acc_track (iter->old_spte, new_spte, iter->level);@@ -644,9 +712,10 @@ static inline void tdp_mmu_set_spte_no_dirty_log(struct kvm *kvm,continue ; \ else -#define tdp_mmu_for_each_pte (_iter, _mmu, _start, _end) \ - for_each_tdp_pte(_iter, __va(_mmu->root_hpa), \ - _mmu->shadow_root_level, _start, _end) +#define tdp_mmu_for_each_pte (_iter, _mmu, _private, _start, _end) \ + for_each_tdp_pte(_iter, \ + __va((_private) ? _mmu->private_root_hpa : _mmu->root_hpa), \ + _mmu->shadow_root_level, _start, _end)/* * Yield if the MMU lock is contended or this thread needs to return control @@ -731,6 +800,18 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,*/ end =min (end, max_gfn_host); +/* +* Extend [start, end) to include GFN shared bit when TDX is enabled, +* and for shared mapping range. +*/ +if (is_private_sp (root)) { + start =kvm_gfn_private (kvm, start); + end =kvm_gfn_private (kvm, end); + }else { + start =kvm_gfn_shared (kvm, start); + end =kvm_gfn_shared (kvm, end); + } +kvm_lockdep_assert_mmu_lock_held (kvm, shared);rcu_read_lock ();@@ -783,13 +864,18 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,* MMU lock. */ bool __kvm_tdp_mmu_zap_gfn_range (struct kvm *kvm,int as_id,gfn_t start, -gfn_t end,bool can_yield,bool flush) +gfn_t end,bool can_yield,bool flush, +bool zap_private) {struct kvm_mmu_page *root; -for_each_tdp_mmu_root_yield_safe (kvm, root, as_id, false) +for_each_tdp_mmu_root_yield_safe (kvm, root, as_id, false) { +/* Skip private page table if not requested */ +if (!zap_private &&is_private_sp (root)) +continue ; flush =zap_gfn_range (kvm, root, start, end, can_yield, flush, false); + }return flush; }@@ -800,7 +886,7 @@ void kvm_tdp_mmu_zap_all(struct kvm *kvm)int i;for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) - flush =kvm_tdp_mmu_zap_gfn_range (kvm, i,0 ,-1ull , flush); + flush =kvm_tdp_mmu_zap_gfn_range (kvm, i,0 ,-1ull , flush, true);if (flush)kvm_flush_remote_tlbs (kvm);@@ -851,6 +937,13 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm)while (root) { next_root =next_invalidated_root (kvm, root); +/* +* Private table is only torn down when VM is destroyed. +* It is a bug to zap private table here. +*/ +if (WARN_ON (is_private_sp (root))) + goto out; +rcu_read_unlock (); flush =zap_gfn_range (kvm, root,0 ,-1ull , true, flush, true);@@ -865,7 +958,7 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm)rcu_read_lock (); } - +out:rcu_read_unlock ();if (flush)@@ -897,9 +990,16 @@ void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm)struct kvm_mmu_page *root;lockdep_assert_held_write (&kvm->mmu_lock); -list_for_each_entry (root, &kvm->arch.tdp_mmu_roots, link) +list_for_each_entry (root, &kvm->arch.tdp_mmu_roots, link) { +/* +* Skip private root since private page table +* is only torn down when VM is destroyed. +*/ +if (is_private_sp (root)) +continue ;if (refcount_inc_not_zero (&root->tdp_mmu_root_count)) root->role.invalid = true; + } }/* @@ -914,14 +1014,23 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,u64 new_spte;int ret =RET_PF_FIXED ;bool wrprot = false; +unsigned long pte_access =ACC_ALL ;
Quoted 4 lines
arch/x86/kvm/mmu/tdp_mmu.c static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, · line 1022
Quoted 4 lines
arch/x86/kvm/mmu/tdp_mmu.c +3
-0WARN_ON (sp->role.level != fault->goal_level); + +/* TDX shared GPAs are no executable, enforce this for the SDV. */ +if (!kvm_is_private_gfn (vcpu->kvm, iter->gfn))
This should be:
if (kvm_gfn_stolen_mask(vcpu->kvm) && !kvm_is_private_gfn(vcpu->kvm, iter->gfn))
Otherwise, when TDX is disabled, all EPTs are going to be considered as shared non-executable EPTs.
Quoted 185 lines
Quoted 185 lines
arch/x86/kvm/mmu/tdp_mmu.c +49
-13+ pte_access &= ~ACC_EXEC_MASK ; +if (unlikely (!fault->slot)) - new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL ); + new_spte =make_mmio_spte (vcpu, +tdp_iter_gfn_unalias (vcpu->kvm, iter), + pte_access);else - wrprot =make_spte (vcpu, sp, fault->slot,ACC_ALL , iter->gfn, - fault->pfn, iter->old_spte, fault->prefetch, true, - fault->map_writable, &new_spte); + wrprot =make_spte (vcpu, sp, fault->slot, pte_access, +tdp_iter_gfn_unalias (vcpu->kvm, iter), + fault->pfn, iter->old_spte, fault->prefetch, + true, fault->map_writable, &new_spte);if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;@@ -959,7 +1068,8 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, }static bool tdp_mmu_populate_nonleaf ( -struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool account_nx) +struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool is_private, +bool account_nx) {struct kvm_mmu_page *sp;u64 *child_pt;@@ -968,7 +1078,7 @@ static bool tdp_mmu_populate_nonleaf(WARN_ON (is_shadow_present_pte (iter->old_spte));WARN_ON (is_removed_spte (iter->old_spte)); - sp =alloc_tdp_mmu_page (vcpu, iter->gfn, iter->level -1 ); + sp =alloc_tdp_mmu_page (vcpu, iter->gfn, iter->level -1 , is_private); child_pt = sp->spt; new_spte =make_nonleaf_spte (child_pt, !shadow_accessed_mask);@@ -991,6 +1101,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) {struct kvm_mmu *mmu = vcpu->arch.mmu;struct tdp_iter iter; +gfn_t raw_gfn; +bool is_private;int ret;kvm_mmu_hugepage_adjust (vcpu, fault);@@ -999,7 +1111,17 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)rcu_read_lock (); -tdp_mmu_for_each_pte (iter, mmu, fault->gfn, fault->gfn +1 ) { + raw_gfn =gpa_to_gfn (fault->addr); + is_private =kvm_is_private_gfn (vcpu->kvm, raw_gfn); + +if (is_error_noslot_pfn (fault->pfn) ||kvm_is_reserved_pfn (fault->pfn)) { +if (is_private) { +rcu_read_unlock (); +return -EFAULT ; + } + } + +tdp_mmu_for_each_pte (iter, mmu, is_private, raw_gfn, raw_gfn +1 ) {if (fault->nx_huge_page_workaround_enabled)disallowed_hugepage_adjust (fault, iter.old_spte, iter.level);@@ -1015,6 +1137,12 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) is_large_pte(iter.old_spte)) {if (!tdp_mmu_zap_spte_atomic (vcpu->kvm, &iter))break ; +/* +* TODO: large page support. +* Doesn't support large page for TDX now +*/ +WARN_ON (is_private_spte (&iter.old_spte)); +/* * The iter must explicitly re-read the spte here @@ -1037,7 +1165,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) account_nx = fault->huge_page_disallowed && fault->req_level >= iter.level; -if (!tdp_mmu_populate_nonleaf (vcpu, &iter, account_nx)) +if (!tdp_mmu_populate_nonleaf ( + vcpu, &iter, is_private, account_nx))break ; } }@@ -1058,9 +1187,12 @@ bool kvm_tdp_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range, {struct kvm_mmu_page *root; -for_each_tdp_mmu_root_yield_safe (kvm, root, range->slot->as_id, false) +for_each_tdp_mmu_root_yield_safe (kvm, root, range->slot->as_id, false) { +if (is_private_sp (root)) +continue ; flush =zap_gfn_range (kvm, root, range->start, range->end, - range->may_block, flush, false); + range->may_block, flush, false); + }return flush; }@@ -1513,10 +1645,14 @@ int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes,struct kvm_mmu *mmu = vcpu->arch.mmu;gfn_t gfn = addr >>PAGE_SHIFT ;int leaf =-1 ; +bool is_private =kvm_is_private_gfn (vcpu->kvm, gfn); *root_level = vcpu->arch.mmu->shadow_root_level; -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +if (WARN_ON (is_private)) +return leaf; + +tdp_mmu_for_each_pte (iter, mmu, false, gfn, gfn +1 ) { leaf = iter.level; sptes[leaf] = iter.old_spte; }@@ -1542,12 +1678,16 @@ u64 *kvm_tdp_mmu_fast_pf_get_last_sptep(struct kvm_vcpu *vcpu, u64 addr,struct kvm_mmu *mmu = vcpu->arch.mmu;gfn_t gfn = addr >>PAGE_SHIFT ;tdp_ptep_t sptep =NULL ; +bool is_private =kvm_is_private_gfn (vcpu->kvm, gfn); -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +if (is_private) + goto out; + +tdp_mmu_for_each_pte (iter, mmu, false, gfn, gfn +1 ) { *spte = iter.old_spte; sptep = iter.sptep; } - +out:/* * Perform the rcu_dereference to get the raw spte pointer value since * we are passing it up to fast_page_fault, which is shared with the arch/x86/kvm/mmu/tdp_mmu.h +8
-5@@ -5,7 +5,7 @@#include <linux/kvm_host.h> -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu); +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private);__must_check static inline bool kvm_tdp_mmu_get_root (struct kvm *kvm,struct kvm_mmu_page *root)@@ -20,11 +20,14 @@ void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root, bool shared);bool __kvm_tdp_mmu_zap_gfn_range (struct kvm *kvm,int as_id,gfn_t start, -gfn_t end,bool can_yield,bool flush); +gfn_t end,bool can_yield,bool flush, +bool zap_private);static inline bool kvm_tdp_mmu_zap_gfn_range (struct kvm *kvm,int as_id, -gfn_t start,gfn_t end,bool flush) +gfn_t start,gfn_t end,bool flush, +bool zap_private) { -return __kvm_tdp_mmu_zap_gfn_range (kvm, as_id, start, end, true, flush); +return __kvm_tdp_mmu_zap_gfn_range (kvm, as_id, start, end, true, flush, + zap_private); }static inline bool kvm_tdp_mmu_zap_sp (struct kvm *kvm,struct kvm_mmu_page *sp) {@@ -41,7 +44,7 @@ static inline bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)*/ lockdep_assert_held_write (&kvm->mmu_lock);return __kvm_tdp_mmu_zap_gfn_range (kvm,kvm_mmu_page_as_id (sp), - sp->gfn, end, false, false); + sp->gfn, end, false, false, false); }void kvm_tdp_mmu_zap_all (struct kvm *kvm);virt/kvm/kvm_main.c +0
-0@@ -190,6 +190,7 @@ bool kvm_is_reserved_pfn(kvm_pfn_t pfn)
Quoted 7 lines
virt/kvm/kvm_main.c bool kvm_is_reserved_pfn(kvm_pfn_t pfn) · line 196
Quoted 6 lines
virt/kvm/kvm_main.c +1
-0return true; } +EXPORT_SYMBOL_GPL (kvm_is_reserved_pfn);/* * Switches to specified vcpu, until a matching vcpu_put()
Trying again in plain text mode. Sorry but it looks like I turned it off at some point and forgot about it.
Sagi
Re: [RFC PATCH v5 048/104] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
Quoted 4 lines · Sagi Shahar
Quoted 3 lines · Kai Huang
Quoted 1 line · On Fri, 2022-03-04 at 11:49 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
...
Quoted 14 lines
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, · line 1022
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +4
-0@@ -914,14 +1014,23 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,u64 new_spte;int ret =RET_PF_FIXED ;bool wrprot = false; +unsigned long pte_access =ACC_ALL ;WARN_ON (sp->role.level != fault->goal_level); + +/* TDX shared GPAs are no executable, enforce this for the SDV. */ +if (!kvm_is_private_gfn (vcpu->kvm, iter->gfn))This should be:
if (kvm_gfn_stolen_mask(vcpu->kvm)&& !kvm_is_private_gfn(vcpu->kvm,iter->gfn))
Quoted 2 lines
Otherwise, when TDX is disabled, all EPTs are going to be considered as shared non-executable EPTs.
Oops, will fix it. Thank you for pointing it out.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [RFC PATCH v5 048/104] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
In reply to [email protected]is not .
Quoted 627 lines · On Fri, Mar 4, 2022 at 12:14 PM <[email protected]> wrote:
From: Isaku Yamahata <[email protected]>
Use private EPT to mirror Secure EPT, and On the change of the private EPT entry, invoke
kvm_x86_opshook in__handle_changed_spte()to propagate the change to Secure EPT.On EPT violation, determine which EPT to use, private or shared, based on faulted GPA. When allocating an EPT page, record it (private or shared) in the page role. The private is passed down to the function as an argument as necessary. When the private EPT entry is changed, call the hook.
When zapping EPT, the EPT entry is frozen with the special EPT value that clears the present bit. After the TLB shootdown, the entry is set to the eventual value. On populating the EPT entry, atomically set the entry.
For TDX, TDX SEAMCALL to update Secure EPT in addition to direct access to the private EPT entry. For the zapping case, freeing the EPT entry works. It can call TDX SEAMCALL in addition to TLB shootdown. For populating the private EPT entry, there can be a race condition without further protection
vcpu 1: populating 2M private EPT entry
vcpu 2: populating 4K private EPT entry
vcpu 2: TDX SEAMCALL to update 4K secure EPT => error vcpu 1: TDX SEAMCALL to update 4M secure EPTTo avoid the race, the frozen EPT entry is utilized. Instead of atomic update of the private EPT entry, freeze the entry, call the hook that invokes TDX SEAMCALL, set the entry to the final value (unfreeze).
Support 4K page only at this stage. 2M page support can be done in future patches.
Co-developed-by Kai Huang <[email protected]>Signed-off-by Kai Huang <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 8 + arch/x86/kvm/mmu/mmu.c | 31 +++- arch/x86/kvm/mmu/tdp_iter.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 226 +++++++++++++++++++++++------ arch/x86/kvm/mmu/tdp_mmu.h | 13 +- virt/kvm/kvm_main.c | 1 + 7 files changed, 232 insertions(+), 51 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +2
-0@@ -91,6 +91,8 @@ KVM_X86_OP(set_tss_addr) KVM_X86_OP(set_identity_map_addr) KVM_X86_OP(get_mt_mask) KVM_X86_OP(load_mmu_pgd) +KVM_X86_OP(free_private_sp) +KVM_X86_OP(handle_changed_private_spte) KVM_X86_OP_NULL(has_wbinvd_exit) KVM_X86_OP(get_l2_tsc_offset) KVM_X86_OP(get_l2_tsc_multiplier)arch/x86/include/asm/kvm_host.h +8
-0@@ -433,6 +433,7 @@ struct kvm_mmu { struct kvm_mmu_page *sp);void (*invlpg)(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa);hpa_t root_hpa; +hpa_t private_root_hpa;gpa_t root_pgd;union kvm_mmu_role mmu_role;u8 root_level;@@ -1433,6 +1434,13 @@ struct kvm_x86_ops {void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level); +int (*free_private_sp)(struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *private_sp); +void (*handle_changed_private_spte)( +struct kvm *kvm,gfn_t gfn,enum pg_level level, +kvm_pfn_t old_pfn,bool was_present,bool was_leaf, +kvm_pfn_t new_pfn,bool is_present,bool is_leaf,void *sept_page); +bool (*has_wbinvd_exit)(void );u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);arch/x86/kvm/mmu/mmu.c +29
-2@@ -3422,6 +3422,7 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) {struct kvm_mmu *mmu = vcpu->arch.mmu;u8 shadow_root_level = mmu->shadow_root_level; +gfn_t gfn_stolen =kvm_gfn_stolen_mask (vcpu->kvm);hpa_t root;unsigned i;int r;@@ -3432,7 +3433,11 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) goto out_unlock;if (is_tdp_mmu_enabled (vcpu->kvm)) { - root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu); +if (gfn_stolen && !VALID_PAGE (mmu->private_root_hpa)) { + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, true); + mmu->private_root_hpa = root; + } + root =kvm_tdp_mmu_get_vcpu_root_hpa (vcpu, false); mmu->root_hpa = root; }else if (shadow_root_level >=PT64_ROOT_4LEVEL ) { root =mmu_alloc_root (vcpu,0 ,0 , shadow_root_level, true);@@ -5596,6 +5601,7 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)int i; mmu->root_hpa =INVALID_PAGE ; + mmu->private_root_hpa =INVALID_PAGE ; mmu->root_pgd =0 ;for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ;@@ -5772,6 +5778,10 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)write_unlock (&kvm->mmu_lock); +/* +* For now private root is never invalidate during VM is running, +* so this can only happen for shared roots. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock);kvm_tdp_mmu_zap_invalidated_roots (kvm);@@ -5871,7 +5881,8 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)if (is_tdp_mmu_enabled (kvm)) {for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) flush =kvm_tdp_mmu_zap_gfn_range (kvm, i, gfn_start, - gfn_end, flush); + gfn_end, flush, + false); }if (flush)@@ -5904,6 +5915,11 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* For now this can only happen for non-TD VM, because TD private +* mapping doesn't support write protection. kvm_tdp_mmu_wrprot_slot() +* will give a WARN() if it hits for TD. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock); flush |=kvm_tdp_mmu_wrprot_slot (kvm, memslot, start_level);@@ -5952,6 +5968,9 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, sp =sptep_to_sp (sptep); pfn =spte_to_pfn (*sptep); +/* Private page dirty logging is not supported. */ +KVM_BUG_ON (is_private_spte (sptep), kvm); +/* * We cannot do huge page mapping for indirect shadow pages, * which are found on the last rmap (level = 1) when not using @@ -5992,6 +6011,11 @@ void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,write_unlock (&kvm->mmu_lock); } +/* +* This should only be reachable in case of log-dirty, wihch TD private +* mapping doesn't support so far. kvm_tdp_mmu_zap_collapsible_sptes() +* internally gives a WARN() when it hits. +*/ if (is_tdp_mmu_enabled (kvm)) {read_lock (&kvm->mmu_lock);kvm_tdp_mmu_zap_collapsible_sptes (kvm, slot);@@ -6266,6 +6290,9 @@ int kvm_mmu_module_init(void)void kvm_mmu_destroy (struct kvm_vcpu *vcpu) {kvm_mmu_unload (vcpu); +if (is_tdp_mmu_enabled (vcpu->kvm)) +mmu_free_root_page (vcpu->kvm, &vcpu->arch.mmu->private_root_hpa, +NULL );free_mmu_pages (&vcpu->arch.root_mmu);free_mmu_pages (&vcpu->arch.guest_mmu);mmu_free_memory_caches (vcpu);arch/x86/kvm/mmu/tdp_iter.h +1
-1@@ -28,7 +28,7 @@ struct tdp_iter {tdp_ptep_t pt_path[PT64_ROOT_MAX_LEVEL ];/* A pointer to the current SPTE */ tdp_ptep_t sptep; -/* The lowest GFN mapped by the current SPTE */ +/* The lowest GFN (stolen bits included) mapped by the current SPTE */ gfn_t gfn;/* The level of the root page given to the iterator */ int root_level;arch/x86/kvm/mmu/tdp_mmu.c +159
-37@@ -53,6 +53,11 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)rcu_barrier (); } +static gfn_t tdp_iter_gfn_unalias (struct kvm *kvm,struct tdp_iter *iter) +{ +return kvm_gfn_unalias (kvm, iter->gfn); +} +static bool zap_gfn_range (struct kvm *kvm,struct kvm_mmu_page *root,gfn_t start,gfn_t end,bool can_yield,bool flush,bool shared);@@ -175,10 +180,13 @@ static union kvm_mmu_page_role page_role_for_level(struct kvm_vcpu *vcpu, }static struct kvm_mmu_page *alloc_tdp_mmu_page (struct kvm_vcpu *vcpu,gfn_t gfn, -int level) +int level,bool private) {struct kvm_mmu_page *sp; +WARN_ON (level != vcpu->arch.mmu->shadow_root_level && +kvm_is_private_gfn (vcpu->kvm, gfn) != private); +WARN_ON (level == vcpu->arch.mmu->shadow_root_level && gfn !=0 ); sp =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_page_header_cache); sp->spt =kvm_mmu_memory_cache_alloc (&vcpu->arch.mmu_shadow_page_cache);set_page_private (virt_to_page (sp->spt), (unsigned long )sp);@@ -186,14 +194,19 @@ static struct kvm_mmu_page *alloc_tdp_mmu_page(struct kvm_vcpu *vcpu, gfn_t gfn, sp->role.word =page_role_for_level (vcpu, level).word; sp->gfn = gfn; sp->tdp_mmu_page = true; -kvm_mmu_init_private_sp (sp); + +if (private) +kvm_mmu_alloc_private_sp (vcpu, sp); +else +kvm_mmu_init_private_sp (sp);trace_kvm_mmu_get_page (sp, true);return sp; } -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu) +static struct kvm_mmu_page *kvm_tdp_mmu_get_vcpu_root (struct kvm_vcpu *vcpu, +bool private) {union kvm_mmu_page_role role;struct kvm *kvm = vcpu->kvm;@@ -206,11 +219,13 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)/* Check for an existing root before allocating a new one. */ for_each_tdp_mmu_root (kvm, root,kvm_mmu_role_as_id (role)) {if (root->role.word == role.word && +is_private_sp (root) == private &&kvm_tdp_mmu_get_root (kvm, root)) goto out; } - root =alloc_tdp_mmu_page (vcpu,0 , vcpu->arch.mmu->shadow_root_level); + root =alloc_tdp_mmu_page (vcpu,0 , vcpu->arch.mmu->shadow_root_level, + private);refcount_set (&root->tdp_mmu_root_count,1 );spin_lock (&kvm->arch.tdp_mmu_pages_lock);@@ -218,12 +233,17 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)spin_unlock (&kvm->arch.tdp_mmu_pages_lock); out: -return __pa (root->spt); +return root; +} + +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private) +{ +return __pa (kvm_tdp_mmu_get_vcpu_root (vcpu, private)->spt); }static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared); +bool private_spte,u64 old_spte, +u64 new_spte,int level,bool shared);static void handle_changed_spte_acc_track (u64 old_spte,u64 new_spte,int level) {@@ -321,6 +341,7 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,int level = sp->role.level;gfn_t base_gfn = sp->gfn;int i; +bool private_sp =is_private_sp (sp);trace_kvm_mmu_prepare_zap_page (sp);@@ -370,7 +391,7 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,*/ WRITE_ONCE (*sptep,SHADOW_REMOVED_SPTE ); } -handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, +handle_changed_spte (kvm,kvm_mmu_page_as_id (sp), gfn, private_sp, old_child_spte,SHADOW_REMOVED_SPTE , level, shared); }@@ -378,6 +399,17 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,kvm_flush_remote_tlbs_with_address (kvm, base_gfn,KVM_PAGES_PER_HPAGE (level +1 )); +if (private_sp && +WARN_ON (static_call (kvm_x86_free_private_sp)( + kvm, sp->gfn, sp->role.level, +kvm_mmu_private_sp (sp)))) { +/* +* Failed to unlink Secure EPT page and there is nothing to do +* further. Intentionally leak the page to prevent the kernel +* from accessing the encrypted page. +*/ +kvm_mmu_init_private_sp (sp); + }call_rcu (&sp->rcu_head, tdp_mmu_free_sp_rcu_callback); }@@ -386,6 +418,7 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,* @kvm: kvm instance * @as_id: the address space of the paging structure the SPTE was a part of * @gfn: the base GFN that was mapped by the SPTE +* @private_spte: the SPTE is private or not * @old_spte: The value of the SPTE before the change * @new_spte: The value of the SPTE after the change * @level: the level of the PT the SPTE is part of in the paging structure @@ -397,14 +430,16 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt,* This function must be called for all TDP SPTE modifications. */ static void __handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared) +bool private_spte,u64 old_spte, +u64 new_spte,int level,bool shared) {bool was_present =is_shadow_present_pte (old_spte);bool is_present =is_shadow_present_pte (new_spte);bool was_leaf = was_present &&is_last_spte (old_spte, level);bool is_leaf = is_present &&is_last_spte (new_spte, level); -bool pfn_changed =spte_to_pfn (old_spte) !=spte_to_pfn (new_spte); +kvm_pfn_t old_pfn =spte_to_pfn (old_spte); +kvm_pfn_t new_pfn =spte_to_pfn (new_spte); +bool pfn_changed = old_pfn != new_pfn;WARN_ON (level >PT64_ROOT_MAX_LEVEL );WARN_ON (level <PG_LEVEL_4K );@@ -468,23 +503,49 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,if (was_leaf &&is_dirty_spte (old_spte) && (!is_present || !is_dirty_spte (new_spte) || pfn_changed)) -kvm_set_pfn_dirty (spte_to_pfn (old_spte)); +kvm_set_pfn_dirty (old_pfn); + +/* +* Special handling for the private mapping. We are either +* setting up new mapping at middle level page table, or leaf, +* or tearing down existing mapping. +*/ +if (private_spte) { +void *sept_page =NULL ; + +if (is_present && !is_leaf) { +struct kvm_mmu_page *sp =to_shadow_page (pfn_to_hpa (new_pfn)); + + sept_page =kvm_mmu_private_sp (sp); +WARN_ON (!sept_page); +WARN_ON (sp->role.level +1 != level); +WARN_ON (sp->gfn != gfn); + } + +static_call (kvm_x86_handle_changed_private_spte)( + kvm, gfn, level, + old_pfn, was_present, was_leaf, + new_pfn, is_present, is_leaf, sept_page); + }/* * Recursively handle child PTs if the change removed a subtree from * the paging structure. */ -if (was_present && !was_leaf && (pfn_changed || !is_present)) +if (was_present && !was_leaf && (pfn_changed || !is_present)) { +WARN_ON (private_spte != +is_private_spte (spte_to_child_pt (old_spte, level)));handle_removed_tdp_mmu_page (kvm,spte_to_child_pt (old_spte, level), shared); + } }static void handle_changed_spte (struct kvm *kvm,int as_id,gfn_t gfn, -u64 old_spte,u64 new_spte,int level, -bool shared) +bool private_spte,u64 old_spte,u64 new_spte, +int level,bool shared) { -__handle_changed_spte (kvm, as_id, gfn, old_spte, new_spte, level, - shared); +__handle_changed_spte (kvm, as_id, gfn, private_spte, + old_spte, new_spte, level, shared);handle_changed_spte_acc_track (old_spte, new_spte, level);handle_changed_spte_dirty_log (kvm, as_id, gfn, old_spte, new_spte, level);@@ -505,6 +566,10 @@ static inline bool tdp_mmu_set_spte_atomic(struct kvm *kvm,struct tdp_iter *iter, u64 new_spte) { + bool freeze_spte = is_private_spte(iter->sptep) && + !is_removed_spte (new_spte); +u64 tmp_spte = freeze_spte ?SHADOW_REMOVED_SPTE : new_spte; +WARN_ON_ONCE (iter->yielded);lockdep_assert_held_read (&kvm->mmu_lock);@@ -521,13 +586,16 @@ static inline bool tdp_mmu_set_spte_atomic(struct kvm *kvm,* does not hold the mmu_lock. */ if (cmpxchg64 (rcu_dereference (iter->sptep), iter->old_spte, - new_spte) != iter->old_spte) + tmp_spte) != iter->old_spte)return false; -__handle_changed_spte (kvm, iter->as_id, iter->gfn, iter->old_spte, - new_spte, iter->level, true); +__handle_changed_spte (kvm, iter->as_id, iter->gfn,is_private_spte (iter->sptep), + iter->old_spte, new_spte, iter->level, true);handle_changed_spte_acc_track (iter->old_spte, new_spte, iter->level); +if (freeze_spte) +WRITE_ONCE (*rcu_dereference (iter->sptep), new_spte); +return true; }@@ -603,8 +671,8 @@ static inline void __tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter,WRITE_ONCE (*rcu_dereference (iter->sptep), new_spte); -__handle_changed_spte (kvm, iter->as_id, iter->gfn, iter->old_spte, - new_spte, iter->level, false); +__handle_changed_spte (kvm, iter->as_id, iter->gfn,is_private_spte (iter->sptep), + iter->old_spte, new_spte, iter->level, false);if (record_acc_track)handle_changed_spte_acc_track (iter->old_spte, new_spte, iter->level);@@ -644,9 +712,10 @@ static inline void tdp_mmu_set_spte_no_dirty_log(struct kvm *kvm,continue ; \ else -#define tdp_mmu_for_each_pte (_iter, _mmu, _start, _end) \ - for_each_tdp_pte(_iter, __va(_mmu->root_hpa), \ - _mmu->shadow_root_level, _start, _end) +#define tdp_mmu_for_each_pte (_iter, _mmu, _private, _start, _end) \ + for_each_tdp_pte(_iter, \ + __va((_private) ? _mmu->private_root_hpa : _mmu->root_hpa), \ + _mmu->shadow_root_level, _start, _end)/* * Yield if the MMU lock is contended or this thread needs to return control @@ -731,6 +800,18 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,*/ end =min (end, max_gfn_host); +/* +* Extend [start, end) to include GFN shared bit when TDX is enabled, +* and for shared mapping range. +*/ +if (is_private_sp (root)) { + start =kvm_gfn_private (kvm, start); + end =kvm_gfn_private (kvm, end); + }else { + start =kvm_gfn_shared (kvm, start); + end =kvm_gfn_shared (kvm, end); + } +kvm_lockdep_assert_mmu_lock_held (kvm, shared);rcu_read_lock ();@@ -783,13 +864,18 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,* MMU lock. */ bool __kvm_tdp_mmu_zap_gfn_range (struct kvm *kvm,int as_id,gfn_t start, -gfn_t end,bool can_yield,bool flush) +gfn_t end,bool can_yield,bool flush, +bool zap_private) {struct kvm_mmu_page *root; -for_each_tdp_mmu_root_yield_safe (kvm, root, as_id, false) +for_each_tdp_mmu_root_yield_safe (kvm, root, as_id, false) { +/* Skip private page table if not requested */ +if (!zap_private &&is_private_sp (root)) +continue ; flush =zap_gfn_range (kvm, root, start, end, can_yield, flush, false); + }return flush; }@@ -800,7 +886,7 @@ void kvm_tdp_mmu_zap_all(struct kvm *kvm)int i;for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) - flush =kvm_tdp_mmu_zap_gfn_range (kvm, i,0 ,-1ull , flush); + flush =kvm_tdp_mmu_zap_gfn_range (kvm, i,0 ,-1ull , flush, true);if (flush)kvm_flush_remote_tlbs (kvm);@@ -851,6 +937,13 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm)while (root) { next_root =next_invalidated_root (kvm, root); +/* +* Private table is only torn down when VM is destroyed. +* It is a bug to zap private table here. +*/ +if (WARN_ON (is_private_sp (root))) + goto out; +rcu_read_unlock (); flush =zap_gfn_range (kvm, root,0 ,-1ull , true, flush, true);@@ -865,7 +958,7 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm)rcu_read_lock (); } - +out:rcu_read_unlock ();if (flush)@@ -897,9 +990,16 @@ void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm)struct kvm_mmu_page *root;lockdep_assert_held_write (&kvm->mmu_lock); -list_for_each_entry (root, &kvm->arch.tdp_mmu_roots, link) +list_for_each_entry (root, &kvm->arch.tdp_mmu_roots, link) { +/* +* Skip private root since private page table +* is only torn down when VM is destroyed. +*/ +if (is_private_sp (root)) +continue ;if (refcount_inc_not_zero (&root->tdp_mmu_root_count)) root->role.invalid = true; + } }/* @@ -914,14 +1014,23 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,u64 new_spte;int ret =RET_PF_FIXED ;bool wrprot = false; +unsigned long pte_access =ACC_ALL ;WARN_ON (sp->role.level != fault->goal_level); + +/* TDX shared GPAs are no executable, enforce this for the SDV. */ +if (!kvm_is_private_gfn (vcpu->kvm, iter->gfn)) + pte_access &= ~ACC_EXEC_MASK ; +if (unlikely (!fault->slot)) - new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL ); + new_spte =make_mmio_spte (vcpu, +tdp_iter_gfn_unalias (vcpu->kvm, iter), + pte_access);else - wrprot =make_spte (vcpu, sp, fault->slot,ACC_ALL , iter->gfn, - fault->pfn, iter->old_spte, fault->prefetch, true, - fault->map_writable, &new_spte); + wrprot =make_spte (vcpu, sp, fault->slot, pte_access, +tdp_iter_gfn_unalias (vcpu->kvm, iter), + fault->pfn, iter->old_spte, fault->prefetch, + true, fault->map_writable, &new_spte);if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;@@ -959,7 +1068,8 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, }static bool tdp_mmu_populate_nonleaf ( -struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool account_nx) +struct kvm_vcpu *vcpu,struct tdp_iter *iter,bool is_private, +bool account_nx) {struct kvm_mmu_page *sp;u64 *child_pt;@@ -968,7 +1078,7 @@ static bool tdp_mmu_populate_nonleaf(WARN_ON (is_shadow_present_pte (iter->old_spte));WARN_ON (is_removed_spte (iter->old_spte)); - sp =alloc_tdp_mmu_page (vcpu, iter->gfn, iter->level -1 ); + sp =alloc_tdp_mmu_page (vcpu, iter->gfn, iter->level -1 , is_private); child_pt = sp->spt; new_spte =make_nonleaf_spte (child_pt, !shadow_accessed_mask);@@ -991,6 +1101,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) {struct kvm_mmu *mmu = vcpu->arch.mmu;struct tdp_iter iter; +gfn_t raw_gfn; +bool is_private;int ret;kvm_mmu_hugepage_adjust (vcpu, fault);@@ -999,7 +1111,17 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)rcu_read_lock (); -tdp_mmu_for_each_pte (iter, mmu, fault->gfn, fault->gfn +1 ) { + raw_gfn =gpa_to_gfn (fault->addr); + is_private =kvm_is_private_gfn (vcpu->kvm, raw_gfn); + +if (is_error_noslot_pfn (fault->pfn) ||kvm_is_reserved_pfn (fault->pfn)) { +if (is_private) { +rcu_read_unlock (); +return -EFAULT ; + } + } + +tdp_mmu_for_each_pte (iter, mmu, is_private, raw_gfn, raw_gfn +1 ) {if (fault->nx_huge_page_workaround_enabled)disallowed_hugepage_adjust (fault, iter.old_spte, iter.level);
arch/x86/kvm/mmu/tdp_mmu.c int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 1144
Quoted 8 lines
arch/x86/kvm/mmu/tdp_mmu.c +5
-0@@ -1015,6 +1137,12 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) is_large_pte(iter.old_spte)) {if (!tdp_mmu_zap_spte_atomic (vcpu->kvm, &iter))break ; +/* +* TODO: large page support. +* Doesn't support large page for TDX now +*/ +WARN_ON (is_private_spte (&iter.old_spte));
The above line is causing a null ptr dereferencing when running the KVM unit tests.
It should be is_private_spte(iter.sptep) instead of is_private_spte(&iter.old_spte)
While old_spte holds a snapshot of the value pointed to by sptep, &old_spte is not equivalent to sptep.
Quoted 119 lines
arch/x86/kvm/mmu/tdp_mmu.c +1
-0+/* * The iter must explicitly re-read the spte here
arch/x86/kvm/mmu/tdp_mmu.c +18
-6@@ -1037,7 +1165,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) account_nx = fault->huge_page_disallowed && fault->req_level >= iter.level; -if (!tdp_mmu_populate_nonleaf (vcpu, &iter, account_nx)) +if (!tdp_mmu_populate_nonleaf ( + vcpu, &iter, is_private, account_nx))break ; } }@@ -1058,9 +1187,12 @@ bool kvm_tdp_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range, {struct kvm_mmu_page *root; -for_each_tdp_mmu_root_yield_safe (kvm, root, range->slot->as_id, false) +for_each_tdp_mmu_root_yield_safe (kvm, root, range->slot->as_id, false) { +if (is_private_sp (root)) +continue ; flush =zap_gfn_range (kvm, root, range->start, range->end, - range->may_block, flush, false); + range->may_block, flush, false); + }return flush; }@@ -1513,10 +1645,14 @@ int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes,struct kvm_mmu *mmu = vcpu->arch.mmu;gfn_t gfn = addr >>PAGE_SHIFT ;int leaf =-1 ; +bool is_private =kvm_is_private_gfn (vcpu->kvm, gfn); *root_level = vcpu->arch.mmu->shadow_root_level; -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +if (WARN_ON (is_private)) +return leaf; + +tdp_mmu_for_each_pte (iter, mmu, false, gfn, gfn +1 ) { leaf = iter.level; sptes[leaf] = iter.old_spte; }@@ -1542,12 +1678,16 @@ u64 *kvm_tdp_mmu_fast_pf_get_last_sptep(struct kvm_vcpu *vcpu, u64 addr,struct kvm_mmu *mmu = vcpu->arch.mmu;gfn_t gfn = addr >>PAGE_SHIFT ;tdp_ptep_t sptep =NULL ; +bool is_private =kvm_is_private_gfn (vcpu->kvm, gfn); -tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { +if (is_private) + goto out; + +tdp_mmu_for_each_pte (iter, mmu, false, gfn, gfn +1 ) { *spte = iter.old_spte; sptep = iter.sptep; } - +out:/* * Perform the rcu_dereference to get the raw spte pointer value since * we are passing it up to fast_page_fault, which is shared with the arch/x86/kvm/mmu/tdp_mmu.h +8
-5@@ -5,7 +5,7 @@#include <linux/kvm_host.h> -hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu); +hpa_t kvm_tdp_mmu_get_vcpu_root_hpa (struct kvm_vcpu *vcpu,bool private);__must_check static inline bool kvm_tdp_mmu_get_root (struct kvm *kvm,struct kvm_mmu_page *root)@@ -20,11 +20,14 @@ void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root, bool shared);bool __kvm_tdp_mmu_zap_gfn_range (struct kvm *kvm,int as_id,gfn_t start, -gfn_t end,bool can_yield,bool flush); +gfn_t end,bool can_yield,bool flush, +bool zap_private);static inline bool kvm_tdp_mmu_zap_gfn_range (struct kvm *kvm,int as_id, -gfn_t start,gfn_t end,bool flush) +gfn_t start,gfn_t end,bool flush, +bool zap_private) { -return __kvm_tdp_mmu_zap_gfn_range (kvm, as_id, start, end, true, flush); +return __kvm_tdp_mmu_zap_gfn_range (kvm, as_id, start, end, true, flush, + zap_private); }static inline bool kvm_tdp_mmu_zap_sp (struct kvm *kvm,struct kvm_mmu_page *sp) {@@ -41,7 +44,7 @@ static inline bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)*/ lockdep_assert_held_write (&kvm->mmu_lock);return __kvm_tdp_mmu_zap_gfn_range (kvm,kvm_mmu_page_as_id (sp), - sp->gfn, end, false, false); + sp->gfn, end, false, false, false); }void kvm_tdp_mmu_zap_all (struct kvm *kvm);virt/kvm/kvm_main.c +1
-1@@ -190,6 +190,7 @@ bool kvm_is_reserved_pfn(kvm_pfn_t pfn)return true; } +EXPORT_SYMBOL_GPL (kvm_is_reserved_pfn);/* * Switches to specified vcpu, until a matching vcpu_put() --
Quoted 2 lines
2.25.1
Sagi
Sean Christopherson
maintainerRe: [RFC PATCH v5 048/104] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
Quoted 30 lines · Sagi Shahar
Quoted 30 lines
arch/x86/kvm/mmu/tdp_mmu.c +24
-1@@ -468,23 +503,49 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,if (was_leaf &&is_dirty_spte (old_spte) && (!is_present || !is_dirty_spte (new_spte) || pfn_changed)) -kvm_set_pfn_dirty (spte_to_pfn (old_spte)); +kvm_set_pfn_dirty (old_pfn); + +/* +* Special handling for the private mapping. We are either +* setting up new mapping at middle level page table, or leaf, +* or tearing down existing mapping. +*/ +if (private_spte) { +void *sept_page =NULL ; + +if (is_present && !is_leaf) { +struct kvm_mmu_page *sp =to_shadow_page (pfn_to_hpa (new_pfn)); + + sept_page =kvm_mmu_private_sp (sp); +WARN_ON (!sept_page); +WARN_ON (sp->role.level +1 != level); +WARN_ON (sp->gfn != gfn); + } + +static_call (kvm_x86_handle_changed_private_spte)( + kvm, gfn, level, + old_pfn, was_present, was_leaf, + new_pfn, is_present, is_leaf, sept_page); + }
Quoted 8 lines
arch/x86/kvm/mmu/tdp_mmu.c static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn, · line 537
Quoted 8 lines
arch/x86/kvm/mmu/tdp_mmu.c +3
-1/* * Recursively handle child PTs if the change removed a subtree from * the paging structure. */ -if (was_present && !was_leaf && (pfn_changed || !is_present)) +if (was_present && !was_leaf && (pfn_changed || !is_present)) { +WARN_ON (private_spte != +is_private_spte (spte_to_child_pt (old_spte, level)));
This sanity check is pointless. The private flag comes from the parent shadow page role, and that's not changing.
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/mmu/tdp_mmu.c +4
-0@@ -1015,6 +1137,12 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) is_large_pte(iter.old_spte)) {if (!tdp_mmu_zap_spte_atomic (vcpu->kvm, &iter))break ; +/* +* TODO: large page support. +* Doesn't support large page for TDX now +*/
Quoted 8 lines
arch/x86/kvm/mmu/tdp_mmu.c int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) · line 1144
Quoted 1 line
arch/x86/kvm/mmu/tdp_mmu.c +1
-0+WARN_ON (is_private_spte (&iter.old_spte));The above line is causing a null ptr dereferencing when running the KVM unit tests.
It should beis_private_spte(iter.sptep)instead ofis_private_spte(&iter.old_spte)
Whileold_spteholds a snapshot of the value pointed to by sptep,&old_spteis not equivalent to sptep.
Bug aside, the name is really, really bad. All of the existing helpers with an "is_blah_spte()" name take an SPTE value, not a pointer to an SPTE.
is_private_sptep() is the obvious choice. That makes me a bit nervous too, and I don't love having to go back to the parent to query private vs shared.
That said, I think it's worth waiting to see the next version of this series before going behind the bikeshed, I suspect many/most of the calls will go away, i.e. we might find a better option presents itself.
RFC PATCH v5 49/104 KVM: x86/tdp_mmu: Ignore unsupported mmu operation on private GFNs
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Some KVM MMU operations (dirty page logging, page migration, aging page) aren't supported for private GFNs (yet). Silently return on unsupported TDX KVM MMU operations.
For dirty logging, aging page, the GFN should be recorded with un-aliased GFN. Insert GFN un-aliasing where necessary.
--- arch/x86/kvm/mmu/tdp_mmu.c | 100 +++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +96 -4
@@ -257,11 +257,20 @@ static void handle_changed_spte_acc_track(u64 old_spte, u64 new_spte, int level) }static void handle_changed_spte_dirty_log (struct kvm *kvm,int as_id,gfn_t gfn, +bool private_spte,u64 old_spte,u64 new_spte,int level) {bool pfn_changed;struct kvm_memory_slot *slot; +/* +* TDX doesn't support live migration. Never mark private page as +* dirty in log-dirty bitmap, since it's not possible for userspace +* hypervisor to live migrate private page anyway. +*/ +if (private_spte) +return ; +if (level >PG_LEVEL_4K )return ;
@@ -269,6 +278,8 @@ static void handle_changed_spte_dirty_log(struct kvm *kvm, int as_id, gfn_t gfn,if ((!is_writable_pte (old_spte) || pfn_changed) &&is_writable_pte (new_spte)) { +/* For memory slot operations, use GFN without aliasing */ + gfn =kvm_gfn_unalias (kvm, gfn); slot =__gfn_to_memslot (__kvm_memslots (kvm, as_id), gfn);mark_page_dirty_in_slot (kvm, slot, gfn); }
@@ -547,7 +558,7 @@ static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,__handle_changed_spte (kvm, as_id, gfn, private_spte, old_spte, new_spte, level, shared);handle_changed_spte_acc_track (old_spte, new_spte, level); -handle_changed_spte_dirty_log (kvm, as_id, gfn, old_spte, +handle_changed_spte_dirty_log (kvm, as_id, gfn, private_spte, old_spte, new_spte, level); }
@@ -678,6 +689,7 @@ static inline void __tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter, iter->level);if (record_dirty_log)handle_changed_spte_dirty_log (kvm, iter->as_id, iter->gfn, +is_private_spte (iter->sptep), iter->old_spte, new_spte, iter->level); }
@@ -1215,7 +1227,23 @@ static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm,* into this helper allow blocking; it'd be dead, wasteful code. */ for_each_tdp_mmu_root (kvm, root, range->slot->as_id) { -tdp_root_for_each_leaf_pte (iter, root, range->start, range->end) +/* +* For TDX shared mapping, set GFN shared bit to the range, +* so the handler() doesn't need to set it, to avoid duplicated +* code in multiple handler()s. +*/ +gfn_t start; +gfn_t end; + +if (is_private_sp (root)) { + start =kvm_gfn_private (kvm, range->start); + end =kvm_gfn_private (kvm, range->end); + }else { + start =kvm_gfn_shared (kvm, range->start); + end =kvm_gfn_shared (kvm, range->end); + } + +tdp_root_for_each_leaf_pte (iter, root, start, end) ret |=handler (kvm, &iter, range); }
@@ -1237,6 +1265,15 @@ static bool age_gfn_range(struct kvm *kvm, struct tdp_iter *iter,if (!is_accessed_spte (iter->old_spte))return false; +/* +* First TDX generation doesn't support clearing A bit for private +* mapping, since there's no secure EPT API to support it. However +* it's a legitimate request for TDX guest, so just return w/o a +* WARN(). +*/ +if (is_private_spte (iter->sptep)) +return false; + new_spte = iter->old_spte;if (spte_ad_enabled (new_spte)) {
@@ -1281,6 +1318,13 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,/* Huge pages aren't expected to be modified without first being zapped. */ WARN_ON (pte_huge (range->pte) || range->start +1 != range->end); +/* +* .change_pte() callback should not happen for private page, because +* for now TDX private pages are pinned during VM's life time. +*/ +if (WARN_ON (is_private_spte (iter->sptep))) +return false; +if (iter->level !=PG_LEVEL_4K || !is_shadow_present_pte (iter->old_spte))return false;
@@ -1332,6 +1376,16 @@ static bool wrprot_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,u64 new_spte;bool spte_set = false; +/* +* First TDX generation doesn't support write protecting private +* mappings, since there's no secure EPT API to support it. It +* is a bug to reach here for TDX guest. +*/ +if (WARN_ON (is_private_sp (root))) +return spte_set; + start =kvm_gfn_shared (kvm, start); + end =kvm_gfn_shared (kvm, end); +rcu_read_lock ();BUG_ON (min_level >KVM_MAX_HUGEPAGE_LEVEL );
@@ -1398,6 +1452,16 @@ static bool clear_dirty_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,u64 new_spte;bool spte_set = false; +/* +* First TDX generation doesn't support clearing dirty bit, +* since there's no secure EPT API to support it. It is a +* bug to reach here for TDX guest. +*/ +if (WARN_ON (is_private_sp (root))) +return spte_set; + start =kvm_gfn_shared (kvm, start); + end =kvm_gfn_shared (kvm, end); +rcu_read_lock ();tdp_root_for_each_leaf_pte (iter, root, start, end) {
@@ -1467,6 +1531,15 @@ static void clear_dirty_pt_masked(struct kvm *kvm, struct kvm_mmu_page *root,struct tdp_iter iter;u64 new_spte; +/* +* First TDX generation doesn't support clearing dirty bit, +* since there's no secure EPT API to support it. It is a +* bug to reach here for TDX guest. +*/ +if (WARN_ON (is_private_sp (root))) +return ; + gfn =kvm_gfn_shared (kvm, gfn); +rcu_read_lock (); tdp_root_for_each_leaf_pte(iter, root, gfn + __ffs(mask),
@@ -1530,6 +1603,16 @@ static void zap_collapsible_spte_range(struct kvm *kvm,struct tdp_iter iter;kvm_pfn_t pfn; +/* +* This should only be reachable in case of log-dirty, which TD +* private mapping doesn't support so far. Give a WARN() if it +* hits private mapping. +*/ +if (WARN_ON (is_private_sp (root))) +return ; + start =kvm_gfn_shared (kvm, start); + end =kvm_gfn_shared (kvm, end); +rcu_read_lock ();tdp_root_for_each_pte (iter, root, start, end) {
@@ -1543,8 +1626,9 @@ static void zap_collapsible_spte_range(struct kvm *kvm, pfn =spte_to_pfn (iter.old_spte);if (kvm_is_reserved_pfn (pfn) || - iter.level >=kvm_mmu_max_mapping_level (kvm, slot, iter.gfn, - pfn,PG_LEVEL_NUM )) + iter.level >=kvm_mmu_max_mapping_level (kvm, slot, +tdp_iter_gfn_unalias (kvm, &iter), pfn, +PG_LEVEL_NUM ))continue ;/* Note, a successful atomic zap also does a remote TLB flush. */
@@ -1590,6 +1674,14 @@ static bool write_protect_gfn(struct kvm *kvm, struct kvm_mmu_page *root,BUG_ON (min_level >KVM_MAX_HUGEPAGE_LEVEL ); +/* +* First TDX generation doesn't support write protecting private +* mappings, since there's no secure EPT API to support it. It +* is a bug to reach here for TDX guest. +*/ +if (WARN_ON (is_private_sp (root))) +return spte_set; +rcu_read_lock (); for_each_tdp_pte_min_level(iter, root->spt, root->role.level,
Signature · 1 line
2.25.1
Paolo Bonzini
maintainerRe: [RFC PATCH v5 049/104] KVM: x86/tdp_mmu: Ignore unsupported mmu operation on private GFNs
Quoted 4 lines · On 3/4/22 20:49, [email protected] wrote:
static void handle_changed_spte_dirty_log(struct kvm *kvm, int
as_id,gfn_tgfn,arch/x86/kvm/mmu/tdp_mmu.c +1
-0+bool private_spte, u64 old_spte, u64 new_spte, int level) {
arch/x86/kvm/mmu/tdp_mmu.c static void handle_changed_spte_acc_track(u64 old_spte, u64 new_spte, int level) · line 272
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +7
-0bool pfn_changed;struct kvm_memory_slot *slot; +/* +* TDX doesn't support live migration. Never mark private page as +* dirty in log-dirty bitmap, since it's not possible for userspace +* hypervisor to live migrate private page anyway. +*/ +if (private_spte) +return ;
This should not be needed, patch 35 will block it anyway because kvm_slot_dirty_track_enabled() will return false.
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +5
-1@@ -1215,7 +1227,23 @@ static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm,* into this helper allow blocking; it'd be dead, wasteful code. */ for_each_tdp_mmu_root (kvm, root, range->slot->as_id) { -tdp_root_for_each_leaf_pte (iter, root, range->start, range->end) +/* +* For TDX shared mapping, set GFN shared bit to the range, +* so the handler() doesn't need to set it, to avoid duplicated +* code in multiple handler()s. +*/
arch/x86/kvm/mmu/tdp_mmu.c static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm, · line 1244
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +10
-0+gfn_t start; +gfn_t end; + +if (is_private_sp (root)) { + start =kvm_gfn_private (kvm, range->start); + end =kvm_gfn_private (kvm, range->end); + }else { + start =kvm_gfn_shared (kvm, range->start); + end =kvm_gfn_shared (kvm, range->end); + }
I think this could be a separate function kvm_gfn_for_root(kvm, root, ...).
arch/x86/kvm/mmu/tdp_mmu.c static bool age_gfn_range(struct kvm *kvm, struct tdp_iter *iter, · line 1275
Quoted 12 lines
arch/x86/kvm/mmu/tdp_mmu.c +8
-0@@ -1237,6 +1265,15 @@ static bool age_gfn_range(struct kvm *kvm, struct tdp_iter *iter,if (!is_accessed_spte (iter->old_spte))return false; +/* +* First TDX generation doesn't support clearing A bit for private +* mapping, since there's no secure EPT API to support it. However +* it's a legitimate request for TDX guest, so just return w/o a +* WARN(). +*/ +if (is_private_spte (iter->sptep)) +return false;
Please add instead a "bool only_shared" argument to kvm_tdp_mmu_handle_gfn, since you can check "only_shared && is_private_sp(root)" just once (instead of checking it once per PTE).
Quoted 18 lines
arch/x86/kvm/mmu/tdp_mmu.c +7
-0new_spte = iter->old_spte;if (spte_ad_enabled (new_spte)) {@@ -1281,6 +1318,13 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,/* Huge pages aren't expected to be modified without first being zapped. */ WARN_ON (pte_huge (range->pte) || range->start +1 != range->end); +/* +* .change_pte() callback should not happen for private page, because +* for now TDX private pages are pinned during VM's life time. +*/ +if (WARN_ON (is_private_spte (iter->sptep))) +return false; +if (iter->level !=PG_LEVEL_4K || !is_shadow_present_pte (iter->old_spte))return false;
arch/x86/kvm/mmu/tdp_mmu.c static bool wrprot_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root, · line 1385
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +7
-0@@ -1332,6 +1376,16 @@ static bool wrprot_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,u64 new_spte;bool spte_set = false; +/* +* First TDX generation doesn't support write protecting private +* mappings, since there's no secure EPT API to support it. It +* is a bug to reach here for TDX guest. +*/ +if (WARN_ON (is_private_sp (root))) +return spte_set;
Isn't this function unreachable even for the shared address range? If so, this WARN should be in kvm_tdp_mmu_wrprot_slot, and also it should check if !kvm_arch_dirty_log_supported(kvm).
Review static bool wrprot_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root, · line 1387
Quoted 2 lines
(quoted diff) +2
-0+ start = kvm_gfn_shared(kvm, start); + end = kvm_gfn_shared(kvm, end);
... and then these two lines are unnecessary.
Quoted 4 lines
arch/x86/kvm/mmu/tdp_mmu.c +0
-0rcu_read_lock ();BUG_ON (min_level >KVM_MAX_HUGEPAGE_LEVEL );
arch/x86/kvm/mmu/tdp_mmu.c static bool clear_dirty_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root, · line 1461
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +7
-0@@ -1398,6 +1452,16 @@ static bool clear_dirty_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,u64 new_spte;bool spte_set = false; +/* +* First TDX generation doesn't support clearing dirty bit, +* since there's no secure EPT API to support it. It is a +* bug to reach here for TDX guest. +*/ +if (WARN_ON (is_private_sp (root))) +return spte_set;
Same here, can you check it in kvm_tdp_mmu_clear_dirty_slot?
Review static bool clear_dirty_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root, · line 1463
Quoted 2 lines
(quoted diff) +2
-0+ start = kvm_gfn_shared(kvm, start); + end = kvm_gfn_shared(kvm, end);
Same here.
Quoted 4 lines
arch/x86/kvm/mmu/tdp_mmu.c +0
-0rcu_read_lock ();tdp_root_for_each_leaf_pte (iter, root, start, end) {
arch/x86/kvm/mmu/tdp_mmu.c static void clear_dirty_pt_masked(struct kvm *kvm, struct kvm_mmu_page *root, · line 1540
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +7
-0@@ -1467,6 +1531,15 @@ static void clear_dirty_pt_masked(struct kvm *kvm, struct kvm_mmu_page *root,struct tdp_iter iter;u64 new_spte; +/* +* First TDX generation doesn't support clearing dirty bit, +* since there's no secure EPT API to support it. It is a +* bug to reach here for TDX guest. +*/ +if (WARN_ON (is_private_sp (root))) +return ;
IIRC this is reachable from userspace, so WARN_ON is not possible. But, again please check
if (!kvm_arch_dirty_log_supported(kvm)) return;
in kvm_tdp_mmu_clear_dirty_pt_masked.
arch/x86/kvm/mmu/tdp_mmu.c static void clear_dirty_pt_masked(struct kvm *kvm, struct kvm_mmu_page *root, · line 1541
Quoted 1 line
arch/x86/kvm/mmu/tdp_mmu.c +1
-0+ gfn =kvm_gfn_shared (kvm, gfn);
Also unnecessary, I think.
Quoted 6 lines
arch/x86/kvm/mmu/tdp_mmu.c +0
-0rcu_read_lock (); tdp_root_for_each_leaf_pte(iter, root, gfn + __ffs(mask),@@ -1530,6 +1603,16 @@ static void zap_collapsible_spte_range(struct kvm *kvm,struct tdp_iter iter;kvm_pfn_t pfn;
arch/x86/kvm/mmu/tdp_mmu.c static void zap_collapsible_spte_range(struct kvm *kvm, · line 1614
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +9
-0+/* +* This should only be reachable in case of log-dirty, which TD +* private mapping doesn't support so far. Give a WARN() if it +* hits private mapping. +*/ +if (WARN_ON (is_private_sp (root))) +return ; + start =kvm_gfn_shared (kvm, start); + end =kvm_gfn_shared (kvm, end);
I think this is not a big deal and you can leave it as is. Alternatively, please move the WARN to kvm_tdp_mmu_zap_collapsible_sptes(). It is also only reachable if you can set KVM_MEM_LOG_DIRTY_PAGES in the first place.
Paolo
Quoted 30 lines
arch/x86/kvm/mmu/tdp_mmu.c +11
-2rcu_read_lock ();tdp_root_for_each_pte (iter, root, start, end) {@@ -1543,8 +1626,9 @@ static void zap_collapsible_spte_range(struct kvm *kvm, pfn =spte_to_pfn (iter.old_spte);if (kvm_is_reserved_pfn (pfn) || - iter.level >=kvm_mmu_max_mapping_level (kvm, slot, iter.gfn, - pfn,PG_LEVEL_NUM )) + iter.level >=kvm_mmu_max_mapping_level (kvm, slot, +tdp_iter_gfn_unalias (kvm, &iter), pfn, +PG_LEVEL_NUM ))continue ;/* Note, a successful atomic zap also does a remote TLB flush. */ @@ -1590,6 +1674,14 @@ static bool write_protect_gfn(struct kvm *kvm, struct kvm_mmu_page *root,BUG_ON (min_level >KVM_MAX_HUGEPAGE_LEVEL ); +/* +* First TDX generation doesn't support write protecting private +* mappings, since there's no secure EPT API to support it. It +* is a bug to reach here for TDX guest. +*/ +if (WARN_ON (is_private_sp (root))) +return spte_set; +rcu_read_lock (); for_each_tdp_pte_min_level(iter, root->spt, root->role.level,
RFC PATCH v5 50/104 The start of TDX KVM patch series: TDX EPT violation
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TDX EPT violation.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -19,12 +19,12 @@ Patch Layer status * TDX architectural definitions: Applied * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied -* TDX EPT violation: Not yet +* TDX EPT violation: Applying * TD finalization: Not yet * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA stolen bits: Applied * KVM TDP refactoring for TDX: Applied -* KVM TDP MMU hooks: Applying +* KVM TDP MMU hooks: Applied * KVM TDP MMU MapGPA: Not yet
Signature · 1 line
2.25.1
RFC PATCH v5 51/104 KVM: TDX: TDP MMU TDX support
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Implement hooks of TDP MMU for TDX backend. TLB flush, TLB shootdown, propagating the change private EPT entry to Secure EPT and freeing Secure EPT page.
TLB flush handles both shared EPT and private EPT. It flushes shared EPT same as VMX. It also waits for the TDX TLB shootdown.
For the hook to free Secure EPT page, unlinks the Secure EPT page from the Secure EPT so that the page can be freed to OS.
Propagating the entry change to Secure EPT. The possible entry changes are present -> non-present(zapping) and non-present -> present(population). On population just link the Secure EPT page or the private guest page to the Secure EPT by TDX SEAMCALL.
Because TDP MMU allows concurrent zapping/population, zapping requires synchronous TLB shootdown with the frozen EPT entry. It zaps the secure entry, increments TLB counter, sends IPI to remote vcpus to trigger TLB flush, and then unlinks the private guest page from the Secure EPT.
For simplicity, batched zapping with exclude lock is handled as concurrent zapping. Although it's inefficient, it can be optimized in the future.
--- arch/x86/kvm/vmx/main.c | 40 +++++- arch/x86/kvm/vmx/tdx.c | 246 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 14 +++ arch/x86/kvm/vmx/tdx_ops.h | 3 + arch/x86/kvm/vmx/x86_ops.h | 2 + 5 files changed, 301 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/main.c +36 -4
@@ -89,6 +89,38 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_flush_tlb (vcpu); + +vmx_flush_tlb_all (vcpu); +} + +static void vt_flush_tlb_current (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_flush_tlb (vcpu); + +vmx_flush_tlb_current (vcpu); +} + +static void vt_flush_tlb_gva (struct kvm_vcpu *vcpu,gva_t addr) +{ +if (KVM_BUG_ON (is_td_vcpu (vcpu), vcpu->kvm)) +return ; + +vmx_flush_tlb_gva (vcpu, addr); +} + +static void vt_flush_tlb_guest (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_flush_tlb_guest (vcpu); +} +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {
@@ -162,10 +194,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .set_rflags = vmx_set_rflags, .get_if_flag = vmx_get_if_flag, - .tlb_flush_all = vmx_flush_tlb_all, - .tlb_flush_current = vmx_flush_tlb_current, - .tlb_flush_gva = vmx_flush_tlb_gva, - .tlb_flush_guest = vmx_flush_tlb_guest, + .tlb_flush_all = vt_flush_tlb_all, + .tlb_flush_current = vt_flush_tlb_current, + .tlb_flush_gva = vt_flush_tlb_gva, + .tlb_flush_guest = vt_flush_tlb_guest, .vcpu_pre_run = vmx_vcpu_pre_run, .run = vmx_vcpu_run,
arch/x86/kvm/vmx/tdx.c +246 -0
283 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +14 -0
@@ -29,9 +29,17 @@ struct kvm_tdx {struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES ];bool finalized; +bool tdh_mem_track;u64 tsc_offset;unsigned long tsc_khz; + +/* +* Lock to prevent seamcalls from running concurrently +* when TDP MMU is enabled, because TDP fault handler +* runs concurrently. +*/ +spinlock_t seamcall_lock; };struct vcpu_tdx {
@@ -166,6 +174,12 @@ static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 fielreturn out.r8; } +static __always_inline int pg_level_to_tdx_sept_level(enum pg_level level) +{ +WARN_ON (level ==PG_LEVEL_NONE ); +return level -1 ; +} +#else #define enable_tdx falsestatic inline int tdx_module_setup (void ) {return -ENODEV ; };
arch/x86/kvm/vmx/tdx_ops.h +3 -0
@@ -30,12 +30,14 @@ static inline u64 tdh_mng_addcx(hpa_t tdr, hpa_t addr)static inline u64 tdh_mem_page_add (hpa_t tdr,gpa_t gpa,hpa_t hpa,hpa_t source,struct tdx_module_output *out) { +tdx_clflush_page (hpa);return kvm_seamcall (TDH_MEM_PAGE_ADD , gpa, tdr, hpa, source,0 , out); }static inline u64 tdh_mem_sept_add (hpa_t tdr,gpa_t gpa,int level,hpa_t page,struct tdx_module_output *out) { +tdx_clflush_page (page);return kvm_seamcall (TDH_MEM_SEPT_ADD , gpa | level, tdr, page,0 ,0 , out); }
@@ -48,6 +50,7 @@ static inline u64 tdh_vp_addcx(hpa_t tdvpr, hpa_t addr)static inline u64 tdh_mem_page_aug (hpa_t tdr,gpa_t gpa,hpa_t hpa,struct tdx_module_output *out) { +tdx_clflush_page (hpa);return kvm_seamcall (TDH_MEM_PAGE_AUG , gpa, tdr, hpa,0 ,0 , out); }
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -144,6 +144,7 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); +void tdx_flush_tlb (struct kvm_vcpu *vcpu);void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);#else static inline void tdx_pre_kvm_init(
@@ -163,6 +164,7 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } +static inline void tdx_flush_tlb (struct kvm_vcpu *vcpu) {}static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {}#endif
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 051/104] KVM: TDX: TDP MMU TDX support
Quoted 107 lines · On Fri, 2022-03-04 at 11:49 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Implement hooks of TDP MMU for TDX backend. TLB flush, TLB shootdown, propagating the change private EPT entry to Secure EPT and freeing Secure EPT page.
TLB flush handles both shared EPT and private EPT. It flushes shared EPT same as VMX. It also waits for the TDX TLB shootdown.
For the hook to free Secure EPT page, unlinks the Secure EPT page from the Secure EPT so that the page can be freed to OS.
Propagating the entry change to Secure EPT. The possible entry changes are present -> non-present(zapping) and non-present -> present(population). On population just link the Secure EPT page or the private guest page to the Secure EPT by TDX SEAMCALL.
Because TDP MMU allows concurrent zapping/population, zapping requires synchronous TLB shootdown with the frozen EPT entry. It zaps the secure entry, increments TLB counter, sends IPI to remote vcpus to trigger TLB flush, and then unlinks the private guest page from the Secure EPT.
For simplicity, batched zapping with exclude lock is handled as concurrent zapping. Although it's inefficient, it can be optimized in the future.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 40 +++++- arch/x86/kvm/vmx/tdx.c | 246 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 14 +++ arch/x86/kvm/vmx/tdx_ops.h | 3 + arch/x86/kvm/vmx/x86_ops.h | 2 + 5 files changed, 301 insertions(+), 4 deletions(-)arch/x86/kvm/vmx/main.c +36
-4@@ -89,6 +89,38 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_flush_tlb (vcpu); + +vmx_flush_tlb_all (vcpu); +} + +static void vt_flush_tlb_current (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_flush_tlb (vcpu); + +vmx_flush_tlb_current (vcpu); +} + +static void vt_flush_tlb_gva (struct kvm_vcpu *vcpu,gva_t addr) +{ +if (KVM_BUG_ON (is_td_vcpu (vcpu), vcpu->kvm)) +return ; + +vmx_flush_tlb_gva (vcpu, addr); +} + +static void vt_flush_tlb_guest (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_flush_tlb_guest (vcpu); +} +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {@@ -162,10 +194,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .set_rflags = vmx_set_rflags, .get_if_flag = vmx_get_if_flag, - .tlb_flush_all = vmx_flush_tlb_all, - .tlb_flush_current = vmx_flush_tlb_current, - .tlb_flush_gva = vmx_flush_tlb_gva, - .tlb_flush_guest = vmx_flush_tlb_guest, + .tlb_flush_all = vt_flush_tlb_all, + .tlb_flush_current = vt_flush_tlb_current, + .tlb_flush_gva = vt_flush_tlb_gva, + .tlb_flush_guest = vt_flush_tlb_guest, .vcpu_pre_run = vmx_vcpu_pre_run, .run = vmx_vcpu_run,arch/x86/kvm/vmx/tdx.c +2
-0@@ -5,7 +5,9 @@#include "capabilities.h" #include "x86_ops.h" +#include "mmu.h" #include "tdx.h" +#include "vmx.h" #include "x86.h" #undef pr_fmt
arch/x86/kvm/vmx/tdx.c int tdx_vm_init(struct kvm *kvm) · line 281
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +5
-0@@ -272,6 +274,15 @@ int tdx_vm_init(struct kvm *kvm)int ret, i;u64 err; +/* +* To generate EPT violation to inject #VE instead of EPT MISCONFIG, +* set RWX=0. +*/ +kvm_mmu_set_mmio_spte_mask (kvm,0 ,VMX_EPT_RWX_MASK ,0 );
I literally spent couple of minutes looking for this chunk while I was looking at the patch "KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis".
arch/x86/kvm/vmx/tdx.c int tdx_vm_init(struct kvm *kvm) · line 284
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +3
-0+ +/* TODO: Enable 2mb and 1gb large page support. */ + kvm->arch.tdp_max_page_level =PG_LEVEL_4K ;
Why don't you move this chunk before MMU code change, where you declared large page is not supported many times in the code?
Quoted 25 lines
arch/x86/kvm/vmx/tdx.c +11
-0+/* vCPUs can't be created until after KVM_TDX_INIT_VM. */ kvm->max_vcpus =0 ;@@ -331,6 +342,8 @@ int tdx_vm_init(struct kvm *kvm)tdx_mark_td_page_added (&kvm_tdx->tdcs[i]); } +spin_lock_init (&kvm_tdx->seamcall_lock); +/* * Note, TDH_MNG_INIT cannot be invoked here. TDH_MNG_INIT requires a dedicated * ioctl() to define the configure CPUID values for the TD. @@ -501,6 +514,220 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK ); } +static void __tdx_sept_set_private_spte (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +hpa_t hpa =pfn_to_hpa (pfn); +gpa_t gpa =gfn_to_gpa (gfn); +struct tdx_module_output out; +u64 err;
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 534
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ +if (WARN_ON_ONCE (is_error_noslot_pfn (pfn) ||kvm_is_reserved_pfn (pfn))) +return ; + +/* TODO: handle large pages. */ +if (KVM_BUG_ON (level !=PG_LEVEL_4K , kvm)) +return ; + +/* Pin the page, TDX KVM doesn't yet support page migration. */ +get_page (pfn_to_page (pfn));
I think there are some MMU code change has the logic that private mappings are not zapped during VM's runtime. This logic depends on page being pinned, which you are doing here.
Quoted 95 lines
arch/x86/kvm/vmx/tdx.c +95
-0+ +if (likely (is_td_finalized (kvm_tdx))) { + err =tdh_mem_page_aug (kvm_tdx->tdr.pa, gpa, hpa, &out); +if (KVM_BUG_ON (err, kvm)) +pr_tdx_error (TDH_MEM_PAGE_AUG , err, &out); +return ; + } +} + +static void tdx_sept_set_private_spte (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); + +spin_lock (&kvm_tdx->seamcall_lock); +__tdx_sept_set_private_spte (kvm, gfn, level, pfn); +spin_unlock (&kvm_tdx->seamcall_lock); +} + +static void tdx_sept_drop_private_spte ( +struct kvm *kvm,gfn_t gfn,enum pg_level level,kvm_pfn_t pfn) +{ +int tdx_level =pg_level_to_tdx_sept_level (level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +gpa_t gpa =gfn_to_gpa (gfn); +hpa_t hpa =pfn_to_hpa (pfn); +hpa_t hpa_with_hkid; +struct tdx_module_output out; +u64 err =0 ; + +/* TODO: handle large pages. */ +if (KVM_BUG_ON (level !=PG_LEVEL_4K , kvm)) +return ; + +spin_lock (&kvm_tdx->seamcall_lock); +if (is_hkid_assigned (kvm_tdx)) { + err =tdh_mem_page_remove (kvm_tdx->tdr.pa, gpa, tdx_level, &out); +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_PAGE_REMOVE , err, &out); + goto unlock; + } + + hpa_with_hkid =set_hkid_to_hpa (hpa, (u16 )kvm_tdx->hkid); + err =tdh_phymem_page_wbinvd (hpa_with_hkid); +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_PHYMEM_PAGE_WBINVD , err,NULL ); + goto unlock; + } + }else + err =tdx_reclaim_page ((unsigned long )__va (hpa), hpa); + +unlock: +spin_unlock (&kvm_tdx->seamcall_lock); + +if (!err) +put_page (pfn_to_page (pfn)); +} + +static int tdx_sept_link_private_sp (struct kvm *kvm,gfn_t gfn, +enum pg_level level,void *sept_page) +{ +int tdx_level =pg_level_to_tdx_sept_level (level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +gpa_t gpa =gfn_to_gpa (gfn); +hpa_t hpa =__pa (sept_page); +struct tdx_module_output out; +u64 err; + +spin_lock (&kvm_tdx->seamcall_lock); + err =tdh_mem_sept_add (kvm_tdx->tdr.pa, gpa, tdx_level, hpa, &out); +spin_unlock (&kvm_tdx->seamcall_lock); +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_SEPT_ADD , err, &out); +return -EIO ; + } + +return 0 ; +} + +static void tdx_sept_zap_private_spte (struct kvm *kvm,gfn_t gfn, +enum pg_level level) +{ +int tdx_level =pg_level_to_tdx_sept_level (level); +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +gpa_t gpa =gfn_to_gpa (gfn); +struct tdx_module_output out; +u64 err; + +spin_lock (&kvm_tdx->seamcall_lock); + err =tdh_mem_range_block (kvm_tdx->tdr.pa, gpa, tdx_level, &out); +spin_unlock (&kvm_tdx->seamcall_lock); +if (KVM_BUG_ON (err, kvm)) +pr_tdx_error (TDH_MEM_RANGE_BLOCK , err, &out); +} +
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 639
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+static int tdx_sept_free_private_sp (struct kvm *kvm,gfn_t gfn,enum pg_level level, +void *sept_page) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +int ret; + +/* +* free_private_sp() is (obviously) called when a shadow page is being +* zapped. KVM doesn't (yet) zap private SPs while the TD is active. +*/
I have some memory that you ever said private memory can be zapped when memory slot is moved/deleted?
Quoted 27 lines
arch/x86/kvm/vmx/tdx.c +27
-0+if (KVM_BUG_ON (is_hkid_assigned (to_kvm_tdx (kvm)), kvm)) +return -EINVAL ; + +spin_lock (&kvm_tdx->seamcall_lock); + ret =tdx_reclaim_page ((unsigned long )sept_page,__pa (sept_page)); +spin_unlock (&kvm_tdx->seamcall_lock); + +return ret; +} + +static int tdx_sept_tlb_remote_flush (struct kvm *kvm) +{ +struct kvm_tdx *kvm_tdx; +u64 err; + +if (!is_td (kvm)) +return -EOPNOTSUPP ; + + kvm_tdx =to_kvm_tdx (kvm); +if (!is_hkid_assigned (kvm_tdx)) +return 0 ; + +/* If TD isn't finalized, it's before any vcpu running. */ +if (unlikely (!is_td_finalized (kvm_tdx))) +return 0 ; + + kvm_tdx->tdh_mem_track = true;
arch/x86/kvm/vmx/tdx.c void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level) · line 676
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ +kvm_make_all_cpus_request (kvm,KVM_REQ_TLB_FLUSH ); + + err =tdh_mem_track (kvm_tdx->tdr.pa); +if (KVM_BUG_ON (err, kvm)) +pr_tdx_error (TDH_MEM_TRACK , err,NULL ); + +WRITE_ONCE (kvm_tdx->tdh_mem_track, false); + +return 0 ;
The whole TLB flush mechanism definitely needs more explanation in either commit message, or comments. How can people understand this magic with such little information?
Quoted 99 lines
arch/x86/kvm/vmx/tdx.c +73
-0+} + +static void tdx_handle_changed_private_spte ( +struct kvm *kvm,gfn_t gfn,enum pg_level level, +kvm_pfn_t old_pfn,bool was_present,bool was_leaf, +kvm_pfn_t new_pfn,bool is_present,bool is_leaf,void *sept_page) +{ +WARN_ON (!is_td (kvm)); +lockdep_assert_held (&kvm->mmu_lock); + +if (is_present) { +/* TDP MMU doesn't change present -> present */ +WARN_ON (was_present); + +/* +* Use different call to either set up middle level +* private page table, or leaf. +*/ +if (is_leaf) +tdx_sept_set_private_spte (kvm, gfn, level, new_pfn); +else { +WARN_ON (!sept_page); +if (tdx_sept_link_private_sp (kvm, gfn, level, sept_page)) +/* failed to update Secure-EPT. */ +WARN_ON (1 ); + } + }else if (was_leaf) { +/* non-present -> non-present doesn't make sense. */ +WARN_ON (!was_present); + +/* +* Zap private leaf SPTE. Zapping private table is done +* below in handle_removed_tdp_mmu_page(). +*/ +tdx_sept_zap_private_spte (kvm, gfn, level); + +/* +* TDX requires TLB tracking before dropping private page. Do +* it here, although it is also done later. +* If hkid isn't assigned, the guest is destroying and no vcpu +* runs further. TLB shootdown isn't needed. +* +* TODO: implement with_range version for optimization. +* kvm_flush_remote_tlbs_with_address(kvm, gfn, 1); +* => tdx_sept_tlb_remote_flush_with_range(kvm, gfn, +* KVM_PAGES_PER_HPAGE(level)); +*/ +if (is_hkid_assigned (to_kvm_tdx (kvm))) +kvm_flush_remote_tlbs (kvm); + +tdx_sept_drop_private_spte (kvm, gfn, level, old_pfn); + } +} +static int tdx_capabilities (struct kvm *kvm,struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;@@ -736,6 +963,21 @@ static int tdx_td_init(struct kvm *kvm, struct kvm_tdx_cmd *cmd)return ret; } +void tdx_flush_tlb (struct kvm_vcpu *vcpu) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); +struct kvm_mmu *mmu = vcpu->arch.mmu; +u64 root_hpa = mmu->root_hpa; + +/* Flush the shared EPTP, if it's valid. */ +if (VALID_PAGE (root_hpa)) +ept_sync_context (construct_eptp (vcpu, root_hpa, + mmu->shadow_root_level)); + +while (READ_ONCE (kvm_tdx->tdh_mem_track)) +cpu_relax (); +} +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_tdx_cmd tdx_cmd;@@ -901,6 +1143,10 @@ static int __init __tdx_hardware_setup(struct kvm_x86_ops *x86_ops) hkid_start_pos = boot_cpu_data.x86_phys_bits; hkid_mask =GENMASK_ULL (max_pa -1 , hkid_start_pos); + x86_ops->tlb_remote_flush = tdx_sept_tlb_remote_flush; + x86_ops->free_private_sp = tdx_sept_free_private_sp; + x86_ops->handle_changed_private_spte = tdx_handle_changed_private_spte; +return 0 ; }arch/x86/kvm/vmx/tdx.h +1
-0@@ -29,9 +29,17 @@ struct kvm_tdx {struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES ];bool finalized; +bool tdh_mem_track;
arch/x86/kvm/vmx/tdx.h struct kvm_tdx { · line 42
Quoted 10 lines
arch/x86/kvm/vmx/tdx.h +7
-0u64 tsc_offset;unsigned long tsc_khz; + +/* +* Lock to prevent seamcalls from running concurrently +* when TDP MMU is enabled, because TDP fault handler +* runs concurrently. +*/ +spinlock_t seamcall_lock;
Please also explain why relevant SEAMCALLs cannot run concurrently.
Quoted 21 lines
arch/x86/kvm/vmx/tdx.h +6
-0};struct vcpu_tdx {@@ -166,6 +174,12 @@ static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 fielreturn out.r8; } +static __always_inline int pg_level_to_tdx_sept_level(enum pg_level level) +{ +WARN_ON (level ==PG_LEVEL_NONE ); +return level -1 ; +} +#else #define enable_tdx falsestatic inline int tdx_module_setup (void ) {return -ENODEV ; };arch/x86/kvm/vmx/tdx_ops.h +0
-0
arch/x86/kvm/vmx/tdx_ops.h static inline u64 tdh_mng_addcx(hpa_t tdr, hpa_t addr) · line 33
Quoted 4 lines
arch/x86/kvm/vmx/tdx_ops.h +1
-0@@ -30,12 +30,14 @@ static inline u64 tdh_mng_addcx(hpa_t tdr, hpa_t addr)static inline u64 tdh_mem_page_add (hpa_t tdr,gpa_t gpa,hpa_t hpa,hpa_t source,struct tdx_module_output *out) { +tdx_clflush_page (hpa);
I think those flush change can be done together when those tdh_mem_xx are introduced with a single explanation why flush is needed. You really don't need to do each of them in separate patch.
Quoted 38 lines
arch/x86/kvm/vmx/tdx_ops.h +2
-0return kvm_seamcall (TDH_MEM_PAGE_ADD , gpa, tdr, hpa, source,0 , out); }static inline u64 tdh_mem_sept_add (hpa_t tdr,gpa_t gpa,int level,hpa_t page,struct tdx_module_output *out) { +tdx_clflush_page (page);return kvm_seamcall (TDH_MEM_SEPT_ADD , gpa | level, tdr, page,0 ,0 , out); }@@ -48,6 +50,7 @@ static inline u64 tdh_vp_addcx(hpa_t tdvpr, hpa_t addr)static inline u64 tdh_mem_page_aug (hpa_t tdr,gpa_t gpa,hpa_t hpa,struct tdx_module_output *out) { +tdx_clflush_page (hpa);return kvm_seamcall (TDH_MEM_PAGE_AUG , gpa, tdr, hpa,0 ,0 , out); }arch/x86/kvm/vmx/x86_ops.h +2
-0@@ -144,6 +144,7 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); +void tdx_flush_tlb (struct kvm_vcpu *vcpu);void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);#else static inline void tdx_pre_kvm_init(@@ -163,6 +164,7 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } +static inline void tdx_flush_tlb (struct kvm_vcpu *vcpu) {}static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {}#endif
RFC PATCH v5 52/104 The start of TDX KVM patch series: KVM TDP MMU MapGPA
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of KVM TDP MMU MapGPA.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -1
@@ -11,6 +11,7 @@ What qemu can do - TDX VM TYPE is exposed to Qemu. - Qemu can create/destroy guest of TDX vm type. - Qemu can create/destroy vcpu of TDX vm type. +- Qemu can populate initial guest memory image. Patch Layer status ------------------
@@ -19,7 +20,7 @@ Patch Layer status * TDX architectural definitions: Applied * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied -* TDX EPT violation: Applying +* TDX EPT violation: Applied * TD finalization: Not yet * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet
Signature · 1 line
2.25.1
RFC PATCH v5 53/104 KVM: x86/mmu: steal software usable bit for EPT to represent shared page
In reply to [email protected]From: Isaku Yamahata <[email protected]>
With TDX, all GFNs are private at guest boot time. At run time guest TD can explicitly change it to shared from private or vice-versa by MapGPA hypercall. If it's specified, the given GFN can't be used as otherwise. That's is, if a guest tells KVM that the GFN is shared, it can't be used as private. or vice-versa.
KVM needs to record it. Steal software usable bit for it from MMIO counter.
--- arch/x86/kvm/mmu/spte.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
arch/x86/kvm/mmu/spte.h +12 -3
@@ -14,6 +14,9 @@*/ #define SPTE_MMU_PRESENT_MASK BIT_ULL(11) +/* Masks that used to track for shared GPA **/ +#define SPTE_PRIVATE_PROHIBIT BIT_ULL(62) +/* * TDP SPTES (more specifically, EPT SPTEs) may not have A/D bits, and may also * be restricted to using write-protection (for L2 when CPU dirty logging, i.e.
@@ -124,7 +127,7 @@ static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK));* the memslots generation and is derived as follows: * * Bits 0-7 of the MMIO generation are propagated to spte bits 3-10 -* Bits 8-18 of the MMIO generation are propagated to spte bits 52-62 +* Bits 8-18 of the MMIO generation are propagated to spte bits 52-61 * * The KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS flag is intentionally not included in * the MMIO generation number, as doing so would require stealing a bit from
@@ -138,7 +141,7 @@ static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK));#define MMIO_SPTE_GEN_LOW_END 10#define MMIO_SPTE_GEN_HIGH_START 52 -#define MMIO_SPTE_GEN_HIGH_END 62 +#define MMIO_SPTE_GEN_HIGH_END 61#define MMIO_SPTE_GEN_LOW_MASK GENMASK_ULL(MMIO_SPTE_GEN_LOW_END, \ MMIO_SPTE_GEN_LOW_START)
@@ -151,7 +154,7 @@ static_assert(!(SPTE_MMU_PRESENT_MASK &#define MMIO_SPTE_GEN_HIGH_BITS (MMIO_SPTE_GEN_HIGH_END - MMIO_SPTE_GEN_HIGH_START + 1)/* remember to adjust the comment above as well if you change these */ -static_assert (MMIO_SPTE_GEN_LOW_BITS ==8 &&MMIO_SPTE_GEN_HIGH_BITS ==11 ); +static_assert (MMIO_SPTE_GEN_LOW_BITS ==8 &&MMIO_SPTE_GEN_HIGH_BITS ==10 );#define MMIO_SPTE_GEN_LOW_SHIFT (MMIO_SPTE_GEN_LOW_START - 0)#define MMIO_SPTE_GEN_HIGH_SHIFT (MMIO_SPTE_GEN_HIGH_START - MMIO_SPTE_GEN_LOW_BITS)
@@ -208,6 +211,7 @@ extern u64 __read_mostly shadow_nonpresent_or_rsvd_mask;/* Removed SPTEs must not be misconstrued as shadow present PTEs. */ static_assert (!(REMOVED_SPTE &SPTE_MMU_PRESENT_MASK )); +static_assert (!(REMOVED_SPTE &SPTE_PRIVATE_PROHIBIT ));/* * See above comment around REMOVED_SPTE. SHADOW_REMOVED_SPTE is the actual
@@ -222,6 +226,11 @@ static inline bool is_removed_spte(u64 spte)return spte ==SHADOW_REMOVED_SPTE ; } +static inline bool is_private_prohibit_spte (u64 spte) +{ +return !!(spte &SPTE_PRIVATE_PROHIBIT ); +} +/* * In some cases, we need to preserve the GFN of a non-present or reserved * SPTE when we usurp the upper five bits of the physical address space to
Signature · 1 line
2.25.1
Paolo Bonzini
maintainerRe: [RFC PATCH v5 053/104] KVM: x86/mmu: steal software usable bit for EPT to represent shared page
arch/x86/kvm/mmu/spte.h line 19
Quoted 3 lines · On 3/4/22 20:49, [email protected] wrote:
arch/x86/kvm/mmu/spte.h +3
-0+/* Masks that used to track for shared GPA **/ +#define SPTE_PRIVATE_PROHIBIT BIT_ULL(62) +
Please rename this to SPTE_SHARED_MAPPING_MASK, or even just SPTE_SHARED_MASK.
Paolo
RFC PATCH v5 54/104 KVM: x86/tdp_mmu: Keep PRIVATE_PROHIBIT bit when zapping
In reply to [email protected]From: Isaku Yamahata <[email protected]>
SPTE_PRIVATE_PROHIBIT specifies the share or private GPA is allowed or not. It needs to be kept over zapping the EPT entry. Currently the EPT entry is initialized shadow_init_value unconditionally to clear SPTE_PRIVATE_PROHIBIT bit. To carry SPTE_PRIVATE_PROHIBIT bit, introduce a helper function to get initial value for zapped entry with SPTE_PRIVATE_PROHIBIT bit. Replace shadow_init_value with it.
--- arch/x86/kvm/mmu/tdp_mmu.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +15 -4
@@ -610,6 +610,12 @@ static inline bool tdp_mmu_set_spte_atomic(struct kvm *kvm,return true; } +static u64 shadow_init_spte (u64 old_spte) +{ +return shadow_init_value | + (is_private_prohibit_spte (old_spte) ?SPTE_PRIVATE_PROHIBIT :0 ); +} +static inline bool tdp_mmu_zap_spte_atomic (struct kvm *kvm,struct tdp_iter *iter) {
@@ -641,7 +647,8 @@ static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm,* shadow_init_value (which sets "suppress #VE" bit) so it * can be set when EPT table entries are zapped. */ -WRITE_ONCE (*rcu_dereference (iter->sptep), shadow_init_value); +WRITE_ONCE (*rcu_dereference (iter->sptep), +shadow_init_spte (iter->old_spte));return true; }
@@ -853,7 +860,8 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,if (!shared) {/* see comments in tdp_mmu_zap_spte_atomic() */ -tdp_mmu_set_spte (kvm, &iter, shadow_init_value); +tdp_mmu_set_spte (kvm, &iter, +shadow_init_spte (iter.old_spte)); flush = true; }else if (!tdp_mmu_zap_spte_atomic (kvm, &iter)) {/*
@@ -1038,11 +1046,14 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, new_spte =make_mmio_spte (vcpu,tdp_iter_gfn_unalias (vcpu->kvm, iter), pte_access); - else + else { wrprot =make_spte (vcpu, sp, fault->slot, pte_access,tdp_iter_gfn_unalias (vcpu->kvm, iter), fault->pfn, iter->old_spte, fault->prefetch, true, fault->map_writable, &new_spte); +if (is_private_prohibit_spte (iter->old_spte)) + new_spte |=SPTE_PRIVATE_PROHIBIT ; + }if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;
@@ -1335,7 +1346,7 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,* invariant that the PFN of a present * leaf SPTE can never change. * See __handle_changed_spte(). */ -tdp_mmu_set_spte (kvm, iter, shadow_init_value); +tdp_mmu_set_spte (kvm, iter,shadow_init_spte (iter->old_spte));if (!pte_write (range->pte)) { new_spte = kvm_mmu_changed_pte_notifier_make_spte(iter->old_spte,
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 054/104] KVM: x86/tdp_mmu: Keep PRIVATE_PROHIBIT bit when zapping
Quoted 8 lines · On Fri, 2022-03-04 at 11:49 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
SPTE_PRIVATE_PROHIBITspecifies the share or private GPA is allowed or not. It needs to be kept over zapping the EPT entry. Currently the EPT entry is initializedshadow_init_valueunconditionally to clearSPTE_PRIVATE_PROHIBITbit. To carrySPTE_PRIVATE_PROHIBITbit, introduce a helper function to get initial value for zapped entry withSPTE_PRIVATE_PROHIBITbit. Replaceshadow_init_valuewith it.
Isn't it better to merge patch 53-55, especially 54-55 together?
Quoted 24 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/tdp_mmu.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)arch/x86/kvm/mmu/tdp_mmu.c +6
-0@@ -610,6 +610,12 @@ static inline bool tdp_mmu_set_spte_atomic(struct kvm *kvm,return true; } +static u64 shadow_init_spte (u64 old_spte) +{ +return shadow_init_value | + (is_private_prohibit_spte (old_spte) ?SPTE_PRIVATE_PROHIBIT :0 ); +} +static inline bool tdp_mmu_zap_spte_atomic (struct kvm *kvm,struct tdp_iter *iter) {
arch/x86/kvm/mmu/tdp_mmu.c static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm, · line 654
Quoted 9 lines
arch/x86/kvm/mmu/tdp_mmu.c +2
-1@@ -641,7 +647,8 @@ static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm,* shadow_init_value (which sets "suppress #VE" bit) so it * can be set when EPT table entries are zapped. */ -WRITE_ONCE (*rcu_dereference (iter->sptep), shadow_init_value); +WRITE_ONCE (*rcu_dereference (iter->sptep), +shadow_init_spte (iter->old_spte));return true; }
In this and next patch (54-55), in all the code path, you already have the iter-
Quoted 1 line
sptep, from which you can get the
sp->private_sp, and check using
is_private_sp(). Why do we need this SPTE_PRIVATE_PRORHIBIT bit?
Are you suggesting we can have mixed private/shared mapping under a private_sp?
Quoted 35 lines
arch/x86/kvm/mmu/tdp_mmu.c +7
-3@@ -853,7 +860,8 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,if (!shared) {/* see comments in tdp_mmu_zap_spte_atomic() */ -tdp_mmu_set_spte (kvm, &iter, shadow_init_value); +tdp_mmu_set_spte (kvm, &iter, +shadow_init_spte (iter.old_spte)); flush = true; }else if (!tdp_mmu_zap_spte_atomic (kvm, &iter)) {/* @@ -1038,11 +1046,14 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, new_spte =make_mmio_spte (vcpu,tdp_iter_gfn_unalias (vcpu->kvm, iter), pte_access); - else + else { wrprot =make_spte (vcpu, sp, fault->slot, pte_access,tdp_iter_gfn_unalias (vcpu->kvm, iter), fault->pfn, iter->old_spte, fault->prefetch, true, fault->map_writable, &new_spte); +if (is_private_prohibit_spte (iter->old_spte)) + new_spte |=SPTE_PRIVATE_PROHIBIT ; + }if (new_spte == iter->old_spte) ret =RET_PF_SPURIOUS ;@@ -1335,7 +1346,7 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,* invariant that the PFN of a present * leaf SPTE can never change. * See __handle_changed_spte(). */ -tdp_mmu_set_spte (kvm, iter, shadow_init_value); +tdp_mmu_set_spte (kvm, iter,shadow_init_spte (iter->old_spte));if (!pte_write (range->pte)) { new_spte = kvm_mmu_changed_pte_notifier_make_spte(iter->old_spte,
RFC PATCH v5 55/104 KVM: x86/tdp_mmu: prevent private/shared map based on PRIVATE_PROHIBIT
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Use the bit SPTE_PRIVATE_PROHIBIT in shared and private EPT to determine which mapping, shared or private, is allowed. If requested mapping isn't allowed, return RET_PF_RETRY to wait for other vcpu to change it.
--- arch/x86/kvm/mmu/spte.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu/spte.h +1 -1
@@ -223,7 +223,7 @@ extern u64 __read_mostly shadow_init_value;static inline bool is_removed_spte (u64 spte) { -return spte ==SHADOW_REMOVED_SPTE ; +return (spte & ~SPTE_PRIVATE_PROHIBIT ) ==SHADOW_REMOVED_SPTE ; }static inline bool is_private_prohibit_spte (u64 spte)
arch/x86/kvm/mmu/tdp_mmu.c +19 -3
@@ -1038,9 +1038,25 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,WARN_ON (sp->role.level != fault->goal_level); -/* TDX shared GPAs are no executable, enforce this for the SDV. */ -if (!kvm_is_private_gfn (vcpu->kvm, iter->gfn)) - pte_access &= ~ACC_EXEC_MASK ; +if (kvm_gfn_stolen_mask (vcpu->kvm)) { +if (is_private_spte (iter->sptep)) { +/* +* This GPA is not allowed to map as private. Let +* vcpu loop in page fault until other vcpu change it +* by MapGPA hypercall. +*/ +if (fault->slot && +is_private_prohibit_spte (iter->old_spte)) +return RET_PF_RETRY ; + }else { +/* This GPA is not allowed to map as shared. */ +if (fault->slot && + !is_private_prohibit_spte (iter->old_spte)) +return RET_PF_RETRY ; +/* TDX shared GPAs are no executable, enforce this. */ + pte_access &= ~ACC_EXEC_MASK ; + } + }if (unlikely (!fault->slot)) new_spte = make_mmio_spte(vcpu,
Signature · 1 line
2.25.1
RFC PATCH v5 56/104 KVM: x86/tdp_mmu: implement MapGPA hypercall for TDX
In reply to [email protected]From: Isaku Yamahata <[email protected]>
The TDX Guest-Hypervisor communication interface(GHCI) specification defines MapGPA hypercall for guest TD to request the host VMM to map given GPA range as private or shared.
It means the guest TD uses the GPA as shared (or private). The GPA won't be used as private (or shared). VMM should enforce GPA usage. VMM doesn't have to map the GPA on the hypercall request.
- Zap the aliased region.
If shared (or private) GPA is requested, zap private (or shared) GPA (modulo shared bit).
- Record the request GPA is shared (or private) by SPTE_PRIVATE_PROHIBIT in SPTE in both shared and private EPT tables.
- With SPTE_PRIVATE_PROHIBIT set, a shared GPA is allowed.
- With SPTE_PRIVATE_PROHIBIT cleared, a private GPA is allowed.
The reason to record SPTE_PRIVATE_PROHIBIT in both shared and private EPT is to optimize EPT violation path for normal guest TD execution path and penalize map_gpa hypercall.
If the guest TD faults on not-allowed GPA (modulo shared bit), the KVM doesn't resolve EPT violation and let vcpu retry. vcpu will keep faulting until other vcpu maps the region with MapGPA hypercall. With the initial value of spte(initial shadow_init_value), SPTE_PRIVATE_PROHIBIT is cleared. So the default behavior doesn't change.
- don't map GPA.
The GPA is mapped on the next EPT violation.
--- arch/x86/kvm/mmu.h | 2 + arch/x86/kvm/mmu/mmu.c | 56 ++++++++++ arch/x86/kvm/mmu/tdp_mmu.c | 208 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu/tdp_mmu.h | 3 + 4 files changed, 269 insertions(+)
arch/x86/kvm/mmu.h +2 -0
@@ -305,6 +305,8 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end);int kvm_arch_write_log_dirty (struct kvm_vcpu *vcpu); +int kvm_mmu_map_gpa (struct kvm_vcpu *vcpu,gfn_t *startp,gfn_t end); +int kvm_mmu_post_init_vm (struct kvm *kvm);void kvm_mmu_pre_destroy_vm (struct kvm *kvm);
arch/x86/kvm/mmu/mmu.c +56 -0
62 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.c +208 -0
220 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.h +3 -0
@@ -74,6 +74,9 @@ bool kvm_tdp_mmu_write_protect_gfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, int min_level); +int kvm_tdp_mmu_map_gpa (struct kvm_vcpu *vcpu, +gfn_t *startp,gfn_t end,bool is_private); +static inline void kvm_tdp_mmu_walk_lockless_begin (void ) {rcu_read_lock ();
Signature · 1 line
2.25.1
RFC PATCH v5 57/104 KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX
In reply to [email protected]From: Sean Christopherson <[email protected]>
Introduce a helper to directly (pun intended) fault-in a TDP page without having to go through the full page fault path. This allows TDX to get the resulting pfn and also allows the RET_PF_* enums to stay in mmu.c where they belong.
--- arch/x86/kvm/mmu.h | 3 +++ arch/x86/kvm/mmu/mmu.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+)
arch/x86/kvm/mmu.h +3 -0
@@ -205,6 +205,9 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,return vcpu->arch.mmu->page_fault (vcpu, &fault); } +kvm_pfn_t kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa, +u32 error_code,int max_level); +/* * Currently, we have two sorts of write-protection, a) the first one * write-protects guest page to sync the guest modification, b) another one is
arch/x86/kvm/mmu/mmu.c +38 -0
@@ -4192,6 +4192,44 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)return direct_page_fault (vcpu, fault); } +kvm_pfn_t kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa, +u32 error_code,int max_level) +{ +int r; +struct kvm_page_fault fault = (struct kvm_page_fault ) { + .addr = gpa, + .error_code = error_code, + .exec = error_code &PFERR_FETCH_MASK , + .write = error_code &PFERR_WRITE_MASK , + .present = error_code &PFERR_PRESENT_MASK , + .rsvd = error_code &PFERR_RSVD_MASK , + .user = error_code &PFERR_USER_MASK , + .prefetch = false, + .is_tdp = true, + .nx_huge_page_workaround_enabled =is_nx_huge_page_enabled (), + }; + +if (mmu_topup_memory_caches (vcpu, false)) +return KVM_PFN_ERR_FAULT ; + +/* +* Loop on the page fault path to handle the case where an mmu_notifier +* invalidation triggers RET_PF_RETRY. In the normal page fault path, +* KVM needs to resume the guest in case the invalidation changed any +* of the page fault properties, i.e. the gpa or error code. For this +* path, the gpa and error code are fixed by the caller, and the caller +* expects failure if and only if the page fault can't be fixed. +*/ +do { + fault.max_level = max_level; + fault.req_level =PG_LEVEL_4K ; + fault.goal_level =PG_LEVEL_4K ; + r =direct_page_fault (vcpu, &fault); + }while (r ==RET_PF_RETRY && !is_error_noslot_pfn (fault.pfn)); +return fault.pfn; +} +EXPORT_SYMBOL_GPL (kvm_mmu_map_tdp_page); +static void nonpaging_init_context (struct kvm_mmu *context) { context->page_fault = nonpaging_page_fault;
Signature · 1 line
2.25.1
RFC PATCH v5 58/104 KVM: x86/mmu: Focibly use TDP MMU for TDX
In reply to [email protected]From: Isaku Yamahata <[email protected]>
At this point, TDX supports TDP MMU and doesn't support legacy MMU. Forcibly use TDP MMU for TDX irrelevant of kernel parameter to disable TDP MMU.
--- arch/x86/kvm/mmu/tdp_mmu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/tdp_mmu.c +6 -1
@@ -16,7 +16,12 @@ module_param_named(tdp_mmu, tdp_mmu_enabled, bool, 0644);/* Initializes the TDP MMU for the VM, if enabled. */ bool kvm_mmu_init_tdp_mmu (struct kvm *kvm) { -if (!tdp_enabled || !READ_ONCE (tdp_mmu_enabled)) +/* +* Because TDX supports only TDP MMU, forcibly use TDP MMU in the case +* of TDX. +*/ +if (kvm->arch.vm_type !=KVM_X86_TDX_VM && + (!tdp_enabled || !READ_ONCE (tdp_mmu_enabled)))return false;/* This should not be changed for the lifetime of the VM. */
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 058/104] KVM: x86/mmu: Focibly use TDP MMU for TDX
Quoted 19 lines · On Fri, 2022-03-04 at 11:49 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
At this point, TDX supports TDP MMU and doesn't support legacy MMU. Forcibly use TDP MMU for TDX irrelevant of kernel parameter to disable TDP MMU.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/mmu/tdp_mmu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)arch/x86/kvm/mmu/tdp_mmu.c +0
-0@@ -16,7 +16,12 @@ module_param_named(tdp_mmu, tdp_mmu_enabled, bool, 0644);/* Initializes the TDP MMU for the VM, if enabled. */ bool kvm_mmu_init_tdp_mmu (struct kvm *kvm) {
arch/x86/kvm/mmu/tdp_mmu.c module_param_named(tdp_mmu, tdp_mmu_enabled, bool, 0644); · line 27
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +6
-1-if (!tdp_enabled || !READ_ONCE (tdp_mmu_enabled)) +/* +* Because TDX supports only TDP MMU, forcibly use TDP MMU in the case +* of TDX. +*/ +if (kvm->arch.vm_type !=KVM_X86_TDX_VM && + (!tdp_enabled || !READ_ONCE (tdp_mmu_enabled)))return false;/* This should not be changed for the lifetime of the VM. */
Please move this patch forward before introducing any private/shared mapping support, otherwise nothing prevents you from creating a TD against legacy MMU, which is broken (especially you have allowed userspace to create TD in patch 10 "KVM: TDX: Make TDX VM type supported").
Signature · 2 lines
Thanks, -Kai
RFC PATCH v5 59/104 The start of TDX KVM patch series: TD finalization
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD finalization.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -2
@@ -21,11 +21,11 @@ Patch Layer status * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied * TDX EPT violation: Applied -* TD finalization: Not yet +* TD finalization: Applying * TD vcpu enter/exit: Not yet * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA stolen bits: Applied * KVM TDP refactoring for TDX: Applied * KVM TDP MMU hooks: Applied -* KVM TDP MMU MapGPA: Not yet +* KVM TDP MMU MapGPA: Applied
Signature · 1 line
2.25.1
RFC PATCH v5 60/104 KVM: TDX: Create initial guest memory
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because the guest memory is protected in TDX, the creation of the initial guest memory requires a dedicated TDX module API, tdh_mem_page_add, instead of directly copying the memory contents into the guest memory in the case of the default VM type. KVM MMU page fault handler callback, private_page_add, handles it.
Define new subcommand, KVM_TDX_INIT_MEM_REGION, of VM-scoped KVM_MEMORY_ENCRYPT_OP. It assigns the guest page, copies the initial memory contents into the guest memory, encrypts the guest memory. At the same time, optionally it extends memory measurement of the TDX guest. It calls the KVM MMU page fault(EPT-violation) handler to trigger the callbacks for it.
--- arch/x86/include/uapi/asm/kvm.h | 9 ++ arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/tdx.c | 128 ++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 2 + tools/arch/x86/include/uapi/asm/kvm.h | 9 ++ 5 files changed, 149 insertions(+)
arch/x86/include/uapi/asm/kvm.h +9 -0
@@ -533,6 +533,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 ,KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU , +KVM_TDX_INIT_MEM_REGION , KVM_TDX_CMD_NR_MAX, };
@@ -574,4 +575,12 @@ struct kvm_tdx_init_vm { __u64 reserved[43]; /* must be zero for future extensibility */ }; +#define KVM_TDX_MEASURE_MEMORY_REGION (1UL << 0) + +struct kvm_tdx_init_mem_region { +__u64 source_addr; +__u64 gpa; +__u64 nr_pages; +}; +#endif /* _ASM_X86_KVM_H */
arch/x86/kvm/mmu/mmu.c +1 -0
@@ -5226,6 +5226,7 @@ int kvm_mmu_load(struct kvm_vcpu *vcpu) out:return r; } +EXPORT_SYMBOL (kvm_mmu_load);void kvm_mmu_unload (struct kvm_vcpu *vcpu) {
arch/x86/kvm/vmx/tdx.c +128 -0
164 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +2 -0
@@ -28,6 +28,8 @@ struct kvm_tdx {int cpuid_nent;struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES ]; +hpa_t source_pa; +bool finalized;bool tdh_mem_track;
tools/arch/x86/include/uapi/asm/kvm.h +9 -0
@@ -533,6 +533,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 ,KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU , +KVM_TDX_INIT_MEM_REGION , KVM_TDX_CMD_NR_MAX, };
@@ -574,4 +575,12 @@ struct kvm_tdx_init_vm { __u64 reserved[43]; /* must be zero for future extensibility */ }; +#define KVM_TDX_MEASURE_MEMORY_REGION (1UL << 0) + +struct kvm_tdx_init_mem_region { +__u64 source_addr; +__u64 gpa; +__u64 nr_pages; +}; +#endif /* _ASM_X86_KVM_H */
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 060/104] KVM: TDX: Create initial guest memory
arch/x86/kvm/vmx/tdx.c static void __tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn, · line 569
Quoted 10 lines · On Fri, 2022-03-04 at 11:49 -0800, [email protected] wrote:
arch/x86/kvm/vmx/tdx.c +10
-0+/* +* In case of TDP MMU, fault handler can run concurrently. Note +* 'source_pa' is a TD scope variable, meaning if there are multiple +* threads reaching here with all needing to access 'source_pa', it +* will break. However fortunately this won't happen, because below +* TDH_MEM_PAGE_ADD code path is only used when VM is being created +* before it is running, using KVM_TDX_INIT_MEM_REGION ioctl (which +* always uses vcpu 0's page table and protected by vcpu->mutex). +*/ +WARN_ON (kvm_tdx->source_pa ==INVALID_PAGE );
We can just KVM_BUG_ON() and return here.
Quoted 9 lines
arch/x86/kvm/vmx/tdx.c +9
-0+ source_pa = kvm_tdx->source_pa & ~KVM_TDX_MEASURE_MEMORY_REGION ; + + err =tdh_mem_page_add (kvm_tdx->tdr.pa, gpa, hpa, source_pa, &out); +if (KVM_BUG_ON (err, kvm)) +pr_tdx_error (TDH_MEM_PAGE_ADD , err, &out); +else if ((kvm_tdx->source_pa &KVM_TDX_MEASURE_MEMORY_REGION )) +tdx_measure_page (kvm_tdx, gpa); + + kvm_tdx->source_pa =INVALID_PAGE ;
RFC PATCH v5 61/104 KVM: TDX: Finalize VM initialization
In reply to [email protected]From: Isaku Yamahata <[email protected]>
To protect the initial contents of the guest TD, the TDX module measures the guest TD during the build process as SHA-384 measurement. The measurement of the guest TD contents needs to be completed to make the guest TD ready to run.
Add a new subcommand, KVM_TDX_FINALIZE_VM, for VM-scoped KVM_MEMORY_ENCRYPT_OP to finalize the measurement and mark the TDX VM ready to run.
--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++++++++++ tools/arch/x86/include/uapi/asm/kvm.h | 1 + 3 files changed, 23 insertions(+)
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -534,6 +534,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU ,KVM_TDX_INIT_MEM_REGION , +KVM_TDX_FINALIZE_VM , KVM_TDX_CMD_NR_MAX, };
arch/x86/kvm/vmx/tdx.c +21 -0
@@ -1103,6 +1103,24 @@ static int tdx_init_mem_region(struct kvm *kvm, struct kvm_tdx_cmd *cmd)return ret; } +static int tdx_td_finalizemr (struct kvm *kvm) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +u64 err; + +if (!is_td_initialized (kvm) ||is_td_finalized (kvm_tdx)) +return -EINVAL ; + + err =tdh_mr_finalize (kvm_tdx->tdr.pa); +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MR_FINALIZE , err,NULL ); +return -EIO ; + } + + kvm_tdx->finalized = true; +return 0 ; +} +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_tdx_cmd tdx_cmd;
@@ -1123,6 +1141,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)case KVM_TDX_INIT_MEM_REGION : r =tdx_init_mem_region (kvm, &tdx_cmd);break ; +case KVM_TDX_FINALIZE_VM : + r =tdx_td_finalizemr (kvm); +break ;default : r = -EINVAL ; goto out;
tools/arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -534,6 +534,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU ,KVM_TDX_INIT_MEM_REGION , +KVM_TDX_FINALIZE_VM , KVM_TDX_CMD_NR_MAX, };
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 061/104] KVM: TDX: Finalize VM initialization
Quoted 74 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To protect the initial contents of the guest TD, the TDX module measures the guest TD during the build process as SHA-384 measurement. The measurement of the guest TD contents needs to be completed to make the guest TD ready to run.
Add a new subcommand,
KVM_TDX_FINALIZE_VM, for VM-scopedKVM_MEMORY_ENCRYPT_OPto finalize the measurement and mark the TDX VM ready to run.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++++++++++ tools/arch/x86/include/uapi/asm/kvm.h | 1 + 3 files changed, 23 insertions(+)arch/x86/include/uapi/asm/kvm.h +1
-0@@ -534,6 +534,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU ,KVM_TDX_INIT_MEM_REGION , +KVM_TDX_FINALIZE_VM , KVM_TDX_CMD_NR_MAX, };arch/x86/kvm/vmx/tdx.c +21
-0@@ -1103,6 +1103,24 @@ static int tdx_init_mem_region(struct kvm *kvm, struct kvm_tdx_cmd *cmd)return ret; } +static int tdx_td_finalizemr (struct kvm *kvm) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +u64 err; + +if (!is_td_initialized (kvm) ||is_td_finalized (kvm_tdx)) +return -EINVAL ; + + err =tdh_mr_finalize (kvm_tdx->tdr.pa); +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MR_FINALIZE , err,NULL ); +return -EIO ; + } + + kvm_tdx->finalized = true; +return 0 ; +} +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_tdx_cmd tdx_cmd;@@ -1123,6 +1141,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)case KVM_TDX_INIT_MEM_REGION : r =tdx_init_mem_region (kvm, &tdx_cmd);break ; +case KVM_TDX_FINALIZE_VM : + r =tdx_td_finalizemr (kvm); +break ;default : r = -EINVAL ; goto out;tools/arch/x86/include/uapi/asm/kvm.h +0
-0
tools/arch/x86/include/uapi/asm/kvm.h enum kvm_tdx_cmd_id { · line 540
Quoted 7 lines
tools/arch/x86/include/uapi/asm/kvm.h +1
-0@@ -534,6 +534,7 @@ enum kvm_tdx_cmd_id {KVM_TDX_INIT_VM ,KVM_TDX_INIT_VCPU ,KVM_TDX_INIT_MEM_REGION , +KVM_TDX_FINALIZE_VM , KVM_TDX_CMD_NR_MAX, };
Note however that errors should be passed back in the struct.
Paolo
RFC PATCH v5 62/104 The start of TDX KVM patch series: TD vcpu enter/exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD vcpu enter/exit.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +3 -2
@@ -12,6 +12,7 @@ What qemu can do - Qemu can create/destroy guest of TDX vm type. - Qemu can create/destroy vcpu of TDX vm type. - Qemu can populate initial guest memory image. +- Qemu can finalize guest TD. Patch Layer status ------------------
@@ -21,8 +22,8 @@ Patch Layer status * TD VM creation/destruction: Applied * TD vcpu creation/destruction: Applied * TDX EPT violation: Applied -* TD finalization: Applying -* TD vcpu enter/exit: Not yet +* TD finalization: Applied +* TD vcpu enter/exit: Applying * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA stolen bits: Applied
Signature · 1 line
2.25.1
RFC PATCH v5 63/104 KVM: TDX: Add helper assembly function to TDX vcpu
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX defines an API to run TDX vcpu with its own ABI. Define an assembly helper function to run TDX vcpu to hide the special ABI so that C code can call it with function call ABI.
--- arch/x86/kvm/vmx/vmenter.S | 146 +++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+)
arch/x86/kvm/vmx/vmenter.S +146 -0
@@ -2,6 +2,7 @@ #include <linux/linkage.h> #include <asm/asm.h> #include <asm/bitsperlong.h> +#include <asm/errno.h> #include <asm/kvm_vcpu_regs.h> #include <asm/nospec-branch.h> #include <asm/segment.h>
@@ -28,6 +29,13 @@ #define VCPU_R15 __VCPU_REGS_R15 * WORD_SIZE #endif +#ifdef CONFIG_INTEL_TDX_HOST +#define TDENTER 0 +#define EXIT_REASON_TDCALL 77 +#define TDENTER_ERROR_BIT 63 +#include "seamcall.h" +#endif + .section .noinstr.text, "ax" /**
@@ -328,3 +336,141 @@ SYM_FUNC_START(vmx_do_interrupt_nmi_irqoff) pop %_ASM_BP RET SYM_FUNC_END(vmx_do_interrupt_nmi_irqoff) + +#ifdef CONFIG_INTEL_TDX_HOST + +.pushsection .noinstr.text, "ax" + +/** + * __tdx_vcpu_run - Call SEAMCALL(TDENTER) to run a TD vcpu + * @tdvpr: physical address of TDVPR + * @regs: void * (to registers of TDVCPU) + * @gpr_mask: non-zero if guest registers need to be loaded prior to TDENTER + * + * Returns: + * TD-Exit Reason + * + * Note: KVM doesn't support using XMM in its hypercalls, it's the HyperV + * code's responsibility to save/restore XMM registers on TDVMCALL. + */ +SYM_FUNC_START(__tdx_vcpu_run) + push %rbp + mov %rsp, %rbp + + push %r15 + push %r14 + push %r13 + push %r12 + push %rbx + + /* Save @regs, which is needed after TDENTER to capture output. */ + push %rsi + + /* Load @tdvpr to RCX */ + mov %rdi, %rcx + + /* No need to load guest GPRs if the last exit wasn't a TDVMCALL. */ + test %dx, %dx + je 1f + + /* Load @regs to RAX, which will be clobbered with $TDENTER anyways. */ + mov %rsi, %rax + + mov VCPU_RBX(%rax), %rbx + mov VCPU_RDX(%rax), %rdx + mov VCPU_RBP(%rax), %rbp + mov VCPU_RSI(%rax), %rsi + mov VCPU_RDI(%rax), %rdi + + mov VCPU_R8 (%rax), %r8 + mov VCPU_R9 (%rax), %r9 + mov VCPU_R10(%rax), %r10 + mov VCPU_R11(%rax), %r11 + mov VCPU_R12(%rax), %r12 + mov VCPU_R13(%rax), %r13 + mov VCPU_R14(%rax), %r14 + mov VCPU_R15(%rax), %r15 + + /* Load TDENTER to RAX. This kills the @regs pointer! */ +1: mov $TDENTER, %rax + +2: seamcall + + /* Skip to the exit path if TDENTER failed. */ + bt $TDENTER_ERROR_BIT, %rax + jc 4f + + /* Temporarily save the TD-Exit reason. */ + push %rax + + /* check if TD-exit due to TDVMCALL */ + cmp $EXIT_REASON_TDCALL, %ax + + /* Reload @regs to RAX. */ + mov 8(%rsp), %rax + + /* Jump on non-TDVMCALL */ + jne 3f + + /* Save all output from SEAMCALL(TDENTER) */ + mov %rbx, VCPU_RBX(%rax) + mov %rbp, VCPU_RBP(%rax) + mov %rsi, VCPU_RSI(%rax) + mov %rdi, VCPU_RDI(%rax) + mov %r10, VCPU_R10(%rax) + mov %r11, VCPU_R11(%rax) + mov %r12, VCPU_R12(%rax) + mov %r13, VCPU_R13(%rax) + mov %r14, VCPU_R14(%rax) + mov %r15, VCPU_R15(%rax) + +3: mov %rcx, VCPU_RCX(%rax) + mov %rdx, VCPU_RDX(%rax) + mov %r8, VCPU_R8 (%rax) + mov %r9, VCPU_R9 (%rax) + + /* + * Clear all general purpose registers except RSP and RAX to prevent + * speculative use of the guest's values. + */ + xor %rbx, %rbx + xor %rcx, %rcx + xor %rdx, %rdx + xor %rsi, %rsi + xor %rdi, %rdi + xor %rbp, %rbp + xor %r8, %r8 + xor %r9, %r9 + xor %r10, %r10 + xor %r11, %r11 + xor %r12, %r12 + xor %r13, %r13 + xor %r14, %r14 + xor %r15, %r15 + + /* Restore the TD-Exit reason to RAX for return. */ + pop %rax + + /* "POP" @regs. */ +4: add $8, %rsp + pop %rbx + pop %r12 + pop %r13 + pop %r14 + pop %r15 + + pop %rbp + ret + +5: cmpb $0, kvm_rebooting + je 6f + mov $-EFAULT, %rax + jmp 4b +6: ud2 + _ASM_EXTABLE(2b, 5b) + +SYM_FUNC_END(__tdx_vcpu_run) + +.popsection + +#endif
Signature · 1 line
2.25.1
RFC PATCH v5 64/104 KVM: TDX: Implement TDX vcpu enter/exit path
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This patch implements running TDX vcpu. Once vcpu runs on the logical processor (LP), the TDX vcpu is associated with it. When the TDX vcpu moves to another LP, the TDX vcpu needs to flush its status on the LP. When destroying TDX vcpu, it needs to complete flush and flush cpu memory cache. Track which LP the TDX vcpu run and flush it as necessary.
Do nothing on sched_in event as TDX doesn't support pause loop.
TDX vcpu execution requires restoring PMU debug store after returning back to KVM because the TDX module unconditionally resets the value. To reuse the existing code, export perf_restore_debug_store.
--- arch/x86/kvm/vmx/main.c | 10 +++++++++- arch/x86/kvm/vmx/tdx.c | 34 ++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 33 +++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ arch/x86/kvm/x86.c | 1 + 5 files changed, 79 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/main.c +9 -1
@@ -89,6 +89,14 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static fastpath_t vt_vcpu_run (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_run (vcpu); + +return vmx_vcpu_run (vcpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -200,7 +208,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .tlb_flush_guest = vt_flush_tlb_guest, .vcpu_pre_run = vmx_vcpu_pre_run, - .run = vmx_vcpu_run, + .run = vt_vcpu_run, .handle_exit = vmx_handle_exit, .skip_emulated_instruction = vmx_skip_emulated_instruction, .update_emulated_instruction = vmx_update_emulated_instruction,
arch/x86/kvm/vmx/tdx.c +34 -0
@@ -10,6 +10,9 @@#include "vmx.h" #include "x86.h" +#include <trace/events/kvm.h> +#include "trace.h" +#undef pr_fmt#define pr_fmt (fmt) "tdx: " fmt
@@ -509,6 +512,37 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +u64 __tdx_vcpu_run (hpa_t tdvpr,void *regs,u32 regs_mask); + +static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu, +struct vcpu_tdx *tdx) +{ +guest_enter_irqoff (); + tdx->exit_reason.full =__tdx_vcpu_run (tdx->tdvpr.pa, vcpu->arch.regs,0 ); +guest_exit_irqoff (); +} + +fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (vcpu->kvm->vm_bugged)) { + tdx->exit_reason.full =TDX_NON_RECOVERABLE_VCPU ; +return EXIT_FASTPATH_NONE ; + } + +trace_kvm_entry (vcpu); + +tdx_vcpu_enter_exit (vcpu, tdx); + + vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ; +trace_kvm_exit (vcpu,KVM_ISA_VMX ); + +if (tdx->exit_reason.error || tdx->exit_reason.non_recoverable) +return EXIT_FASTPATH_NONE ; +return EXIT_FASTPATH_NONE ; +} +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK );
arch/x86/kvm/vmx/tdx.h +33 -0
@@ -44,12 +44,45 @@ struct kvm_tdx {spinlock_t seamcall_lock; }; +union tdx_exit_reason { +struct { +/* 31:0 mirror the VMX Exit Reason format */ +u64 basic :16 ; +u64 reserved16 :1 ; +u64 reserved17 :1 ; +u64 reserved18 :1 ; +u64 reserved19 :1 ; +u64 reserved20 :1 ; +u64 reserved21 :1 ; +u64 reserved22 :1 ; +u64 reserved23 :1 ; +u64 reserved24 :1 ; +u64 reserved25 :1 ; +u64 bus_lock_detected :1 ; +u64 enclave_mode :1 ; +u64 smi_pending_mtf :1 ; +u64 smi_from_vmx_root :1 ; +u64 reserved30 :1 ; +u64 failed_vmentry :1 ; + +/* 63:32 are TDX specific */ +u64 details_l1 :8 ; +u64 class :8 ; +u64 reserved61_48 :14 ; +u64 non_recoverable :1 ; +u64 error :1 ; + }; +u64 full; +}; +struct vcpu_tdx {struct kvm_vcpu vcpu;struct tdx_td_page tdvpr;struct tdx_td_page *tdvpx; +union tdx_exit_reason exit_reason; +bool initialized; };
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -140,6 +140,7 @@ void tdx_vm_free(struct kvm *kvm);int tdx_vcpu_create (struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event); +fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -160,6 +161,7 @@ static inline void tdx_vm_free(struct kvm *kvm) {}static inline int tdx_vcpu_create (struct kvm_vcpu *vcpu) {return -EOPNOTSUPP ; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {} +static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; }static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
arch/x86/kvm/x86.c +1 -0
@@ -300,6 +300,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = { };u64 __read_mostly host_xcr0; +EXPORT_SYMBOL_GPL (host_xcr0);u64 __read_mostly supported_xcr0;EXPORT_SYMBOL_GPL (supported_xcr0);
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 064/104] KVM: TDX: Implement TDX vcpu enter/exit path
Quoted 88 lines · On Fri, Mar 4, 2022 at 11:50 AM <[email protected]> wrote:
From: Isaku Yamahata <[email protected]>
This patch implements running TDX vcpu. Once vcpu runs on the logical processor (LP), the TDX vcpu is associated with it. When the TDX vcpu moves to another LP, the TDX vcpu needs to flush its status on the LP. When destroying TDX vcpu, it needs to complete flush and flush cpu memory cache. Track which LP the TDX vcpu run and flush it as necessary.
Do nothing on
sched_inevent as TDX doesn't support pause loop.TDX vcpu execution requires restoring PMU debug store after returning back to KVM because the TDX module unconditionally resets the value. To reuse the existing code, export
perf_restore_debug_store.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 10 +++++++++- arch/x86/kvm/vmx/tdx.c | 34 ++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 33 +++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ arch/x86/kvm/x86.c | 1 + 5 files changed, 79 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/main.c +9
-1@@ -89,6 +89,14 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static fastpath_t vt_vcpu_run (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_run (vcpu); + +return vmx_vcpu_run (vcpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))@@ -200,7 +208,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .tlb_flush_guest = vt_flush_tlb_guest, .vcpu_pre_run = vmx_vcpu_pre_run, - .run = vmx_vcpu_run, + .run = vt_vcpu_run, .handle_exit = vmx_handle_exit, .skip_emulated_instruction = vmx_skip_emulated_instruction, .update_emulated_instruction = vmx_update_emulated_instruction,arch/x86/kvm/vmx/tdx.c +21
-0@@ -10,6 +10,9 @@#include "vmx.h" #include "x86.h" +#include <trace/events/kvm.h> +#include "trace.h" +#undef pr_fmt#define pr_fmt (fmt) "tdx: " fmt@@ -509,6 +512,37 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +u64 __tdx_vcpu_run (hpa_t tdvpr,void *regs,u32 regs_mask); + +static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu, +struct vcpu_tdx *tdx) +{ +guest_enter_irqoff (); + tdx->exit_reason.full =__tdx_vcpu_run (tdx->tdvpr.pa, vcpu->arch.regs,0 ); +guest_exit_irqoff (); +} + +fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (vcpu->kvm->vm_bugged)) { + tdx->exit_reason.full =TDX_NON_RECOVERABLE_VCPU ; +return EXIT_FASTPATH_NONE ; + }
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 542
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ +trace_kvm_entry (vcpu); + +tdx_vcpu_enter_exit (vcpu, tdx); + + vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ; +trace_kvm_exit (vcpu,KVM_ISA_VMX ); + +if (tdx->exit_reason.error || tdx->exit_reason.non_recoverable) +return EXIT_FASTPATH_NONE ;
Looks like the above if statement has no effect. Just checking if this is intentional.
Quoted 91 lines
arch/x86/kvm/vmx/tdx.c +3
-0+return EXIT_FASTPATH_NONE ; +} +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK );arch/x86/kvm/vmx/tdx.h +33
-0@@ -44,12 +44,45 @@ struct kvm_tdx {spinlock_t seamcall_lock; }; +union tdx_exit_reason { +struct { +/* 31:0 mirror the VMX Exit Reason format */ +u64 basic :16 ; +u64 reserved16 :1 ; +u64 reserved17 :1 ; +u64 reserved18 :1 ; +u64 reserved19 :1 ; +u64 reserved20 :1 ; +u64 reserved21 :1 ; +u64 reserved22 :1 ; +u64 reserved23 :1 ; +u64 reserved24 :1 ; +u64 reserved25 :1 ; +u64 bus_lock_detected :1 ; +u64 enclave_mode :1 ; +u64 smi_pending_mtf :1 ; +u64 smi_from_vmx_root :1 ; +u64 reserved30 :1 ; +u64 failed_vmentry :1 ; + +/* 63:32 are TDX specific */ +u64 details_l1 :8 ; +u64 class :8 ; +u64 reserved61_48 :14 ; +u64 non_recoverable :1 ; +u64 error :1 ; + }; +u64 full; +}; +struct vcpu_tdx {struct kvm_vcpu vcpu;struct tdx_td_page tdvpr;struct tdx_td_page *tdvpx; +union tdx_exit_reason exit_reason; +bool initialized; };arch/x86/kvm/vmx/x86_ops.h +2
-0@@ -140,6 +140,7 @@ void tdx_vm_free(struct kvm *kvm);int tdx_vcpu_create (struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event); +fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);@@ -160,6 +161,7 @@ static inline void tdx_vm_free(struct kvm *kvm) {}static inline int tdx_vcpu_create (struct kvm_vcpu *vcpu) {return -EOPNOTSUPP ; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {} +static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; }static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }arch/x86/kvm/x86.c +1
-1@@ -300,6 +300,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = { };u64 __read_mostly host_xcr0; +EXPORT_SYMBOL_GPL (host_xcr0);u64 __read_mostly supported_xcr0;EXPORT_SYMBOL_GPL (supported_xcr0); --2.25.1
Re: [RFC PATCH v5 064/104] KVM: TDX: Implement TDX vcpu enter/exit path
Quoted 28 lines · Erdem Aktas
Quoted 27 lines · On Fri, Mar 4, 2022 at 11:50 AM <[email protected]> wrote:
arch/x86/kvm/vmx/tdx.c +23
-0@@ -509,6 +512,37 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +u64 __tdx_vcpu_run (hpa_t tdvpr,void *regs,u32 regs_mask); + +static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu, +struct vcpu_tdx *tdx) +{ +guest_enter_irqoff (); + tdx->exit_reason.full =__tdx_vcpu_run (tdx->tdvpr.pa, vcpu->arch.regs,0 ); +guest_exit_irqoff (); +} + +fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (vcpu->kvm->vm_bugged)) { + tdx->exit_reason.full =TDX_NON_RECOVERABLE_VCPU ; +return EXIT_FASTPATH_NONE ; + } + +trace_kvm_entry (vcpu); + +tdx_vcpu_enter_exit (vcpu, tdx); +
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 542
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +5
-0+ vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ; +trace_kvm_exit (vcpu,KVM_ISA_VMX ); + +if (tdx->exit_reason.error || tdx->exit_reason.non_recoverable) +return EXIT_FASTPATH_NONE ;Looks like the above if statement has no effect. Just checking if this is intentional.
I'm not sure if I get your point. tdx->exit_reason is updated by the above tdx_cpu_enter_exit(). So it makes sense to check .error or .non_recoverable.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [RFC PATCH v5 064/104] KVM: TDX: Implement TDX vcpu enter/exit path
Quoted 41 lines · Isaku Yamahata
Quoted 28 lines · Erdem Aktas
Quoted 27 lines · On Fri, Mar 4, 2022 at 11:50 AM <[email protected]> wrote:
arch/x86/kvm/vmx/tdx.c +23
-0@@ -509,6 +512,37 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +u64 __tdx_vcpu_run (hpa_t tdvpr,void *regs,u32 regs_mask); + +static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu, +struct vcpu_tdx *tdx) +{ +guest_enter_irqoff (); + tdx->exit_reason.full =__tdx_vcpu_run (tdx->tdvpr.pa, vcpu->arch.regs,0 ); +guest_exit_irqoff (); +} + +fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (vcpu->kvm->vm_bugged)) { + tdx->exit_reason.full =TDX_NON_RECOVERABLE_VCPU ; +return EXIT_FASTPATH_NONE ; + } + +trace_kvm_entry (vcpu); + +tdx_vcpu_enter_exit (vcpu, tdx); +Quoted 8 lines
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 542
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +5
-0+ vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ; +trace_kvm_exit (vcpu,KVM_ISA_VMX ); + +if (tdx->exit_reason.error || tdx->exit_reason.non_recoverable) +return EXIT_FASTPATH_NONE ;Looks like the above if statement has no effect. Just checking if this is intentional.
Quoted 4 lines
I'm not sure if I get your point.
tdx->exit_reasonis updated by the abovetdx_cpu_enter_exit(). So it makes sense to check .error or .non_recoverable. --
Isaku Yamahata <[email protected]>
What I mean is, if there is an error, it returns EXIT_FASTPATH_NONE but if there is no error, it still returns EXIT_FASTPATH_NONE.
The code is like below, the if-statement might be there as a placeholder to check errors but it has no impact on what is returned from this function.
if (tdx->exit_reason.error || tdx->exit_reason.non_recoverable)
return EXIT_FASTPATH_NONE;
return EXIT_FASTPATH_NONE;Re: [RFC PATCH v5 064/104] KVM: TDX: Implement TDX vcpu enter/exit path
Quoted 51 lines · Erdem Aktas
Quoted 41 lines · Isaku Yamahata
Quoted 28 lines · Erdem Aktas
Quoted 27 lines · On Fri, Mar 4, 2022 at 11:50 AM <[email protected]> wrote:
arch/x86/kvm/vmx/tdx.c +23
-0@@ -509,6 +512,37 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +u64 __tdx_vcpu_run (hpa_t tdvpr,void *regs,u32 regs_mask); + +static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu, +struct vcpu_tdx *tdx) +{ +guest_enter_irqoff (); + tdx->exit_reason.full =__tdx_vcpu_run (tdx->tdvpr.pa, vcpu->arch.regs,0 ); +guest_exit_irqoff (); +} + +fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (vcpu->kvm->vm_bugged)) { + tdx->exit_reason.full =TDX_NON_RECOVERABLE_VCPU ; +return EXIT_FASTPATH_NONE ; + } + +trace_kvm_entry (vcpu); + +tdx_vcpu_enter_exit (vcpu, tdx); +Quoted 8 lines
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 542
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +5
-0+ vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ; +trace_kvm_exit (vcpu,KVM_ISA_VMX ); + +if (tdx->exit_reason.error || tdx->exit_reason.non_recoverable) +return EXIT_FASTPATH_NONE ;Looks like the above if statement has no effect. Just checking if this is intentional.
Quoted 4 lines
I'm not sure if I get your point.
tdx->exit_reasonis updated by the abovetdx_cpu_enter_exit(). So it makes sense to check .error or .non_recoverable. --
Isaku Yamahata <[email protected]>What I mean is, if there is an error, it returns
EXIT_FASTPATH_NONEbut if there is no error, it still returnsEXIT_FASTPATH_NONE.
Quoted 7 lines
The code is like below, the if-statement might be there as a placeholder to check errors but it has no impact on what is returned from this function.
if (tdx->exit_reason.error || tdx->exit_reason.non_recoverable) return EXIT_FASTPATH_NONE; return EXIT_FASTPATH_NONE;
Got it. It doesn't make sense. I'll fix it with the next respin.
Signature · 1 line
Isaku Yamahata <[email protected]>
RFC PATCH v5 65/104 KVM: TDX: vcpu_run: save/restore host state(host kernel gs)
In reply to [email protected]From: Isaku Yamahata <[email protected]>
On entering/exiting TDX vcpu, Preserved or clobbered CPU state is different from VMX case. Add TDX hooks to save/restore host/guest CPU state. Save/restore kernel GS base MSR.
--- arch/x86/kvm/vmx/main.c | 28 +++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 39 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 73 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/main.c +26 -2
@@ -89,6 +89,30 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static void vt_prepare_switch_to_guest (struct kvm_vcpu *vcpu) +{ +/* +* All host state is saved/restored across SEAMCALL/SEAMRET, and the +* guest state of a TD is obviously off limits. Deferring MSRs and DRs +* is pointless because the TDX module needs to load *something* so as +* not to expose guest state. +*/ +if (is_td_vcpu (vcpu)) { +tdx_prepare_switch_to_guest (vcpu); +return ; + } + +vmx_prepare_switch_to_guest (vcpu); +} + +static void vt_vcpu_put (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_put (vcpu); + +return vmx_vcpu_put (vcpu); +} +static fastpath_t vt_vcpu_run (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -174,9 +198,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_free = vt_vcpu_free, .vcpu_reset = vt_vcpu_reset, - .prepare_guest_switch = vmx_prepare_switch_to_guest, + .prepare_guest_switch = vt_prepare_switch_to_guest, .vcpu_load = vmx_vcpu_load, - .vcpu_put = vmx_vcpu_put, + .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap, .get_msr_feature = vmx_get_msr_feature,
arch/x86/kvm/vmx/tdx.c +39 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/cpu.h> +#include <linux/mmu_context.h> #include <asm/tdx.h>
@@ -407,6 +408,9 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.guest_state_protected = !(to_kvm_tdx (vcpu->kvm)->attributes &TDX_TD_ATTRIBUTE_DEBUG ); + tdx->host_state_need_save = true; + tdx->host_state_need_restore = false; +return 0 ; free_tdvpx:
@@ -420,6 +424,39 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)return ret; } +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (!tdx->host_state_need_save) +return ; + +if (likely (is_64bit_mm (current->mm))) + tdx->msr_host_kernel_gs_base = current->thread.gsbase; +else + tdx->msr_host_kernel_gs_base =read_msr (MSR_KERNEL_GS_BASE ); + + tdx->host_state_need_save = false; +} + +static void tdx_prepare_switch_to_host (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + + tdx->host_state_need_save = true; +if (!tdx->host_state_need_restore) +return ; + +wrmsrl (MSR_KERNEL_GS_BASE , tdx->msr_host_kernel_gs_base); + tdx->host_state_need_restore = false; +} + +void tdx_vcpu_put (struct kvm_vcpu *vcpu) +{ +vmx_vcpu_pi_put (vcpu); +tdx_prepare_switch_to_host (vcpu); +} +void tdx_vcpu_free (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
@@ -535,6 +572,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx); + tdx->host_state_need_restore = true; + vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX );
arch/x86/kvm/vmx/tdx.h +4 -0
@@ -84,6 +84,10 @@ struct vcpu_tdx {union tdx_exit_reason exit_reason;bool initialized; + +bool host_state_need_save; +bool host_state_need_restore; +u64 msr_host_kernel_gs_base; };static inline bool is_td (struct kvm *kvm)
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -141,6 +141,8 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu); +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu); +void tdx_vcpu_put (struct kvm_vcpu *vcpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -162,6 +164,8 @@ static inline int tdx_vcpu_create(struct kvm_vcpu *vcpu) { return -EOPNOTSUPP; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; } +static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 065/104] KVM: TDX: vcpu_run: save/restore host state(host kernel gs)
Quoted 160 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
On entering/exiting TDX vcpu, Preserved or clobbered CPU state is different from VMX case. Add TDX hooks to save/restore host/guest CPU state. Save/restore kernel GS base MSR.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 28 +++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 39 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 4 ++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ 4 files changed, 73 insertions(+), 2 deletions(-)arch/x86/kvm/vmx/main.c +26
-2@@ -89,6 +89,30 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)return vmx_vcpu_reset (vcpu, init_event); } +static void vt_prepare_switch_to_guest (struct kvm_vcpu *vcpu) +{ +/* +* All host state is saved/restored across SEAMCALL/SEAMRET, and the +* guest state of a TD is obviously off limits. Deferring MSRs and DRs +* is pointless because the TDX module needs to load *something* so as +* not to expose guest state. +*/ +if (is_td_vcpu (vcpu)) { +tdx_prepare_switch_to_guest (vcpu); +return ; + } + +vmx_prepare_switch_to_guest (vcpu); +} + +static void vt_vcpu_put (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_put (vcpu); + +return vmx_vcpu_put (vcpu); +} +static fastpath_t vt_vcpu_run (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))@@ -174,9 +198,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_free = vt_vcpu_free, .vcpu_reset = vt_vcpu_reset, - .prepare_guest_switch = vmx_prepare_switch_to_guest, + .prepare_guest_switch = vt_prepare_switch_to_guest, .vcpu_load = vmx_vcpu_load, - .vcpu_put = vmx_vcpu_put, + .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap, .get_msr_feature = vmx_get_msr_feature,arch/x86/kvm/vmx/tdx.c +39
-0@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/cpu.h> +#include <linux/mmu_context.h> #include <asm/tdx.h> @@ -407,6 +408,9 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.guest_state_protected = !(to_kvm_tdx (vcpu->kvm)->attributes &TDX_TD_ATTRIBUTE_DEBUG ); + tdx->host_state_need_save = true; + tdx->host_state_need_restore = false; +return 0 ; free_tdvpx:@@ -420,6 +424,39 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)return ret; } +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (!tdx->host_state_need_save) +return ; + +if (likely (is_64bit_mm (current->mm))) + tdx->msr_host_kernel_gs_base = current->thread.gsbase; +else + tdx->msr_host_kernel_gs_base =read_msr (MSR_KERNEL_GS_BASE ); + + tdx->host_state_need_save = false; +} + +static void tdx_prepare_switch_to_host (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + + tdx->host_state_need_save = true; +if (!tdx->host_state_need_restore) +return ; + +wrmsrl (MSR_KERNEL_GS_BASE , tdx->msr_host_kernel_gs_base); + tdx->host_state_need_restore = false; +} + +void tdx_vcpu_put (struct kvm_vcpu *vcpu) +{ +vmx_vcpu_pi_put (vcpu); +tdx_prepare_switch_to_host (vcpu); +} +void tdx_vcpu_free (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);@@ -535,6 +572,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx); + tdx->host_state_need_restore = true; + vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX );arch/x86/kvm/vmx/tdx.h +4
-0@@ -84,6 +84,10 @@ struct vcpu_tdx {union tdx_exit_reason exit_reason;bool initialized; + +bool host_state_need_save; +bool host_state_need_restore; +u64 msr_host_kernel_gs_base; };static inline bool is_td (struct kvm *kvm)arch/x86/kvm/vmx/x86_ops.h +2
-0@@ -141,6 +141,8 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu); +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu); +void tdx_vcpu_put (struct kvm_vcpu *vcpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
arch/x86/kvm/vmx/x86_ops.h static inline int tdx_vcpu_create(struct kvm_vcpu *vcpu) { return -EOPNOTSUPP; } · line 171
Quoted 8 lines
arch/x86/kvm/vmx/x86_ops.h +2
-0@@ -162,6 +164,8 @@ static inline int tdx_vcpu_create(struct kvm_vcpu *vcpu) { return -EOPNOTSUPP; }static inline void tdx_vcpu_free (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; } +static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
RFC PATCH v5 66/104 KVM: TDX: restore host xsave state when exit from the guest TD
In reply to [email protected]From: Isaku Yamahata <[email protected]>
On exiting from the guest TD, xsave state is clobbered. Restore xsave state on TD exit.
--- arch/x86/kvm/vmx/tdx.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
arch/x86/kvm/vmx/tdx.c +18 -0
@@ -2,6 +2,7 @@#include <linux/cpu.h> #include <linux/mmu_context.h> +#include <asm/fpu/xcr.h> #include <asm/tdx.h> #include "capabilities.h"
@@ -549,6 +550,22 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); + +if (static_cpu_has (X86_FEATURE_XSAVE ) && + host_xcr0 != (kvm_tdx->xfam & supported_xcr0)) +xsetbv (XCR_XFEATURE_ENABLED_MASK , host_xcr0); +if (static_cpu_has (X86_FEATURE_XSAVES ) && +/* PT can be exposed to TD guest regardless of KVM's XSS support */ + host_xss != (kvm_tdx->xfam & (supported_xss |XFEATURE_MASK_PT ))) +wrmsrl (MSR_IA32_XSS , host_xss); +if (static_cpu_has (X86_FEATURE_PKU ) && + (kvm_tdx->xfam &XFEATURE_MASK_PKRU )) +write_pkru (vcpu->arch.host_pkru); +} +u64 __tdx_vcpu_run (hpa_t tdvpr,void *regs,u32 regs_mask); static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
@@ -572,6 +589,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx); +tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true; vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;
Signature · 1 line
2.25.1
RFC PATCH v5 67/104 KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr
In reply to [email protected]From: Chao Gao <[email protected]>
Several MSRs are constant and only used in userspace(ring 3). But VMs may have different values. KVM uses kvm_set_user_return_msr() to switch to guest's values and leverages user return notifier to restore them when the kernel is to return to userspace. To eliminate unnecessary wrmsr, KVM also caches the value it wrote to an MSR last time.
TDX module unconditionally resets some of these MSRs to architectural INIT state on TD exit. It makes the cached values in kvm_user_return_msrs are inconsistent with values in hardware. This inconsistency needs to be fixed. Otherwise, it may mislead kvm_on_user_return() to skip restoring some MSRs to the host's values. kvm_set_user_return_msr() can help correct this case, but it is not optimal as it always does a wrmsr. So, introduce a variation of kvm_set_user_return_msr() to update cached values and skip that wrmsr.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1894,6 +1894,7 @@ int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,int kvm_add_user_return_msr (u32 msr);int kvm_find_user_return_msr (u32 msr);int kvm_set_user_return_msr (unsigned index,u64 val,u64 mask); +void kvm_user_return_update_cache (unsigned int index,u64 val);static inline bool kvm_is_supported_user_return_msr (u32 msr) {
arch/x86/kvm/x86.c +20 -5
@@ -427,6 +427,15 @@ static void kvm_user_return_msr_cpu_online(void) } } +static void kvm_user_return_register_notifier (struct kvm_user_return_msrs *msrs) +{ +if (!msrs->registered) { + msrs->urn.on_user_return = kvm_on_user_return; +user_return_notifier_register (&msrs->urn); + msrs->registered = true; + } +} +int kvm_set_user_return_msr (unsigned slot,u64 value,u64 mask) {unsigned int cpu =smp_processor_id ();
@@ -441,15 +450,21 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)return 1 ; msrs->values[slot].curr = value; -if (!msrs->registered) { - msrs->urn.on_user_return = kvm_on_user_return; -user_return_notifier_register (&msrs->urn); - msrs->registered = true; - } +kvm_user_return_register_notifier (msrs);return 0 ; }EXPORT_SYMBOL_GPL (kvm_set_user_return_msr); +/* Update the cache, "curr", and register the notifier */ +void kvm_user_return_update_cache (unsigned int slot,u64 value) +{ +struct kvm_user_return_msrs *msrs =this_cpu_ptr (user_return_msrs); + + msrs->values[slot].curr = value; +kvm_user_return_register_notifier (msrs); +} +EXPORT_SYMBOL_GPL (kvm_user_return_update_cache); +static void drop_user_return_notifiers (void ) {unsigned int cpu =smp_processor_id ();
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 067/104] KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr
Quoted 73 lines · On 3/4/22 20:49, [email protected] wrote:
From: Chao Gao <[email protected]>
Several MSRs are constant and only used in userspace(ring 3). But VMs may have different values. KVM uses
kvm_set_user_return_msr()to switch to guest's values and leverages user return notifier to restore them when the kernel is to return to userspace. To eliminate unnecessary wrmsr, KVM also caches the value it wrote to an MSR last time.TDX module unconditionally resets some of these MSRs to architectural INIT state on TD exit. It makes the cached values in
kvm_user_return_msrsare inconsistent with values in hardware. This inconsistency needs to be fixed. Otherwise, it may misleadkvm_on_user_return()to skip restoring some MSRs to the host's values.kvm_set_user_return_msr()can help correct this case, but it is not optimal as it always does a wrmsr. So, introduce a variation ofkvm_set_user_return_msr()to update cached values and skip that wrmsr.Signed-off-by Chao Gao <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -1894,6 +1894,7 @@ int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,int kvm_add_user_return_msr (u32 msr);int kvm_find_user_return_msr (u32 msr);int kvm_set_user_return_msr (unsigned index,u64 val,u64 mask); +void kvm_user_return_update_cache (unsigned int index,u64 val);static inline bool kvm_is_supported_user_return_msr (u32 msr) {arch/x86/kvm/x86.c +13
-5@@ -427,6 +427,15 @@ static void kvm_user_return_msr_cpu_online(void) } } +static void kvm_user_return_register_notifier (struct kvm_user_return_msrs *msrs) +{ +if (!msrs->registered) { + msrs->urn.on_user_return = kvm_on_user_return; +user_return_notifier_register (&msrs->urn); + msrs->registered = true; + } +} +int kvm_set_user_return_msr (unsigned slot,u64 value,u64 mask) {unsigned int cpu =smp_processor_id ();@@ -441,15 +450,21 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)return 1 ; msrs->values[slot].curr = value; -if (!msrs->registered) { - msrs->urn.on_user_return = kvm_on_user_return; -user_return_notifier_register (&msrs->urn); - msrs->registered = true; - } +kvm_user_return_register_notifier (msrs);return 0 ; }EXPORT_SYMBOL_GPL (kvm_set_user_return_msr); +/* Update the cache, "curr", and register the notifier */ +void kvm_user_return_update_cache (unsigned int slot,u64 value) +{
arch/x86/kvm/x86.c int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask) · line 470
Quoted 10 lines
arch/x86/kvm/x86.c +7
-0+struct kvm_user_return_msrs *msrs =this_cpu_ptr (user_return_msrs); + + msrs->values[slot].curr = value; +kvm_user_return_register_notifier (msrs); +} +EXPORT_SYMBOL_GPL (kvm_user_return_update_cache); +static void drop_user_return_notifiers (void ) {unsigned int cpu =smp_processor_id ();
RFC PATCH v5 68/104 KVM: TDX: restore user ret MSRs
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Several user ret MSRs are clobbered on TD exit. Restore those values on TD exit and before returning to ring 3.
--- arch/x86/kvm/vmx/tdx.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
arch/x86/kvm/vmx/tdx.c +33 -0
@@ -550,6 +550,28 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +struct tdx_uret_msr { +u32 msr; +unsigned int slot; +u64 defval; +}; + +static struct tdx_uret_msr tdx_uret_msrs[] = { + {.msr =MSR_SYSCALL_MASK ,}, + {.msr =MSR_STAR ,}, + {.msr =MSR_LSTAR ,}, + {.msr =MSR_TSC_AUX ,}, +}; + +static void tdx_user_return_update_cache (void ) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) +kvm_user_return_update_cache (tdx_uret_msrs[i].slot, + tdx_uret_msrs[i].defval); +} +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm);
@@ -589,6 +611,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx); +tdx_user_return_update_cache ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;
@@ -1371,6 +1394,16 @@ static int __init __tdx_hardware_setup(struct kvm_x86_ops *x86_ops)if (WARN_ON_ONCE (x86_ops->tlb_remote_flush))return -EIO ; +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) { + tdx_uret_msrs[i].slot =kvm_find_user_return_msr (tdx_uret_msrs[i].msr); +if (tdx_uret_msrs[i].slot ==-1 ) { +/* If any MSR isn't supported, it is a KVM bug */ +pr_err ("MSR %x isn't included by kvm_find_user_return_msr\n" , + tdx_uret_msrs[i].msr); +return -EIO ; + } + } + max_pkgs =topology_max_packages (); tdx_mng_key_config_lock =kcalloc (max_pkgs,sizeof (*tdx_mng_key_config_lock),GFP_KERNEL );
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 068/104] KVM: TDX: restore user ret MSRs
Quoted 58 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Several user ret MSRs are clobbered on TD exit. Restore those values on TD exit and before returning to ring 3.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)arch/x86/kvm/vmx/tdx.c +26
-0@@ -550,6 +550,28 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +struct tdx_uret_msr { +u32 msr; +unsigned int slot; +u64 defval; +}; + +static struct tdx_uret_msr tdx_uret_msrs[] = { + {.msr =MSR_SYSCALL_MASK ,}, + {.msr =MSR_STAR ,}, + {.msr =MSR_LSTAR ,}, + {.msr =MSR_TSC_AUX ,}, +}; + +static void tdx_user_return_update_cache (void ) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) +kvm_user_return_update_cache (tdx_uret_msrs[i].slot, + tdx_uret_msrs[i].defval); +} +static void tdx_restore_host_xsave_state (struct kvm_vcpu *vcpu) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm);@@ -589,6 +611,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx); +tdx_user_return_update_cache ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;@@ -1371,6 +1394,16 @@ static int __init __tdx_hardware_setup(struct kvm_x86_ops *x86_ops)if (WARN_ON_ONCE (x86_ops->tlb_remote_flush))return -EIO ; +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) { + tdx_uret_msrs[i].slot =kvm_find_user_return_msr (tdx_uret_msrs[i].msr); +if (tdx_uret_msrs[i].slot ==-1 ) {
arch/x86/kvm/vmx/tdx.c static int __init __tdx_hardware_setup(struct kvm_x86_ops *x86_ops) · line 1409
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +7
-0+/* If any MSR isn't supported, it is a KVM bug */ +pr_err ("MSR %x isn't included by kvm_find_user_return_msr\n" , + tdx_uret_msrs[i].msr); +return -EIO ; + } + } + max_pkgs =topology_max_packages (); tdx_mng_key_config_lock =kcalloc (max_pkgs,sizeof (*tdx_mng_key_config_lock),GFP_KERNEL );
I wonder if you only need to do this if !this_cpu_ptr(user_return_msrs)->registered, but not a big deal.
RFC PATCH v5 69/104 The start of TDX KVM patch series: TD vcpu exits/interrupts/hypercalls
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This empty commit is to mark the start of patch series of TD vcpu exits, interrupts, and hypercalls.
--- Documentation/virt/kvm/intel-tdx-layer-status.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Documentation/virt/kvm/intel-tdx-layer-status.rst +2 -1
@@ -13,6 +13,7 @@ What qemu can do - Qemu can create/destroy vcpu of TDX vm type. - Qemu can populate initial guest memory image. - Qemu can finalize guest TD. +- Qemu can start to run vcpu. But vcpu can not make progress yet. Patch Layer status ------------------
@@ -23,7 +24,7 @@ Patch Layer status * TD vcpu creation/destruction: Applied * TDX EPT violation: Applied * TD finalization: Applied -* TD vcpu enter/exit: Applying +* TD vcpu enter/exit: Applied * TD vcpu interrupts/exit/hypercall: Not yet * KVM MMU GPA stolen bits: Applied
Signature · 1 line
2.25.1
RFC PATCH v5 70/104 KVM: TDX: complete interrupts after tdexit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
This corresponds to VMX __vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care NMI injection.
--- arch/x86/kvm/vmx/tdx.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/x86/kvm/vmx/tdx.c +10 -0
@@ -550,6 +550,14 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) +{ +/* Avoid costly SEAMCALL if no nmi was injected */ +if (vcpu->arch.nmi_injected) + vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu), +TD_VCPU_PEND_NMI ); +} +struct tdx_uret_msr {u32 msr;unsigned int slot;
@@ -618,6 +626,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX ); +tdx_complete_interrupts (vcpu); +if (tdx->exit_reason.error || tdx->exit_reason.non_recoverable)return EXIT_FASTPATH_NONE ;return EXIT_FASTPATH_NONE ;
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 070/104] KVM: TDX: complete interrupts after tdexit
Quoted 30 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
This corresponds to VMX
__vmx_complete_interrupts(). Because TDX virtualize vAPIC, KVM only needs to care NMI injection.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 10 ++++++++++ 1 file changed, 10 insertions(+)arch/x86/kvm/vmx/tdx.c +8
-0@@ -550,6 +550,14 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +static void tdx_complete_interrupts (struct kvm_vcpu *vcpu) +{ +/* Avoid costly SEAMCALL if no nmi was injected */ +if (vcpu->arch.nmi_injected) + vcpu->arch.nmi_injected =td_management_read8 (to_tdx (vcpu), +TD_VCPU_PEND_NMI ); +} +struct tdx_uret_msr {u32 msr;unsigned int slot;
arch/x86/kvm/vmx/tdx.c fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) · line 633
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +2
-0@@ -618,6 +626,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET ;trace_kvm_exit (vcpu,KVM_ISA_VMX ); +tdx_complete_interrupts (vcpu); +if (tdx->exit_reason.error || tdx->exit_reason.non_recoverable)return EXIT_FASTPATH_NONE ;return EXIT_FASTPATH_NONE ;
RFC PATCH v5 71/104 KVM: TDX: restore debug store when TD exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Because debug store is clobbered, restore it on TD exit.
--- arch/x86/events/intel/ds.c | 1 + arch/x86/kvm/vmx/tdx.c | 1 + 2 files changed, 2 insertions(+)
arch/x86/events/intel/ds.c +1 -0
@@ -2256,3 +2256,4 @@ void perf_restore_debug_store(void)wrmsrl (MSR_IA32_DS_AREA , (unsigned long )ds); } +EXPORT_SYMBOL_GPL (perf_restore_debug_store);
arch/x86/kvm/vmx/tdx.c +1 -0
@@ -620,6 +620,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx);tdx_user_return_update_cache (); +perf_restore_debug_store ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 071/104] KVM: TDX: restore debug store when TD exit
Quoted 24 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Because debug store is clobbered, restore it on TD exit.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/events/intel/ds.c | 1 + arch/x86/kvm/vmx/tdx.c | 1 + 2 files changed, 2 insertions(+)arch/x86/events/intel/ds.c +1
-0@@ -2256,3 +2256,4 @@ void perf_restore_debug_store(void)wrmsrl (MSR_IA32_DS_AREA , (unsigned long )ds); } +EXPORT_SYMBOL_GPL (perf_restore_debug_store);arch/x86/kvm/vmx/tdx.c +0
-0
arch/x86/kvm/vmx/tdx.c fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) · line 626
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +1
-0@@ -620,6 +620,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)tdx_vcpu_enter_exit (vcpu, tdx);tdx_user_return_update_cache (); +perf_restore_debug_store ();tdx_restore_host_xsave_state (vcpu); tdx->host_state_need_restore = true;
RFC PATCH v5 72/104 KVM: TDX: handle vcpu migration over logical processor
In reply to [email protected]From: Isaku Yamahata <[email protected]>
For vcpu migration, in the case of VMX, VCMS is flushed on the source pcpu, and load it on the target pcpu. There are corresponding TDX SEAMCALL APIs, call them on vcpu migration. The logic is mostly same as VMX except the TDX SEAMCALLs are used.
--- arch/x86/kvm/vmx/main.c | 20 +++++++++++++-- arch/x86/kvm/vmx/tdx.c | 51 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ 3 files changed, 71 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/main.c +18 -2
@@ -121,6 +121,14 @@ static fastpath_t vt_vcpu_run(struct kvm_vcpu *vcpu)return vmx_vcpu_run (vcpu); } +static void vt_vcpu_load (struct kvm_vcpu *vcpu,int cpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_load (vcpu, cpu); + +return vmx_vcpu_load (vcpu, cpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -162,6 +170,14 @@ static void vt_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); } +static void vt_sched_in (struct kvm_vcpu *vcpu,int cpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_sched_in (vcpu, cpu); +} +static int vt_mem_enc_op (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -199,7 +215,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_reset = vt_vcpu_reset, .prepare_guest_switch = vt_prepare_switch_to_guest, - .vcpu_load = vmx_vcpu_load, + .vcpu_load = vt_vcpu_load, .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap,
@@ -285,7 +301,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .request_immediate_exit = vmx_request_immediate_exit, - .sched_in = vmx_sched_in, + .sched_in = vt_sched_in, .cpu_dirty_log_size = PML_ENTITY_NUM, .update_cpu_dirty_logging = vmx_update_cpu_dirty_logging,
arch/x86/kvm/vmx/tdx.c +51 -0
69 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -143,6 +143,7 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu);void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu); +void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -166,6 +167,7 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; }static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 072/104] KVM: TDX: handle vcpu migration over logical processor
Quoted 155 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
For vcpu migration, in the case of VMX, VCMS is flushed on the source pcpu, and load it on the target pcpu. There are corresponding TDX SEAMCALL APIs, call them on vcpu migration. The logic is mostly same as VMX except the TDX SEAMCALLs are used.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 20 +++++++++++++-- arch/x86/kvm/vmx/tdx.c | 51 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ 3 files changed, 71 insertions(+), 2 deletions(-)arch/x86/kvm/vmx/main.c +18
-2@@ -121,6 +121,14 @@ static fastpath_t vt_vcpu_run(struct kvm_vcpu *vcpu)return vmx_vcpu_run (vcpu); } +static void vt_vcpu_load (struct kvm_vcpu *vcpu,int cpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_load (vcpu, cpu); + +return vmx_vcpu_load (vcpu, cpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))@@ -162,6 +170,14 @@ static void vt_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,vmx_load_mmu_pgd (vcpu, root_hpa, pgd_level); } +static void vt_sched_in (struct kvm_vcpu *vcpu,int cpu) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_sched_in (vcpu, cpu); +} +static int vt_mem_enc_op (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))@@ -199,7 +215,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_reset = vt_vcpu_reset, .prepare_guest_switch = vt_prepare_switch_to_guest, - .vcpu_load = vmx_vcpu_load, + .vcpu_load = vt_vcpu_load, .vcpu_put = vt_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap,@@ -285,7 +301,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .request_immediate_exit = vmx_request_immediate_exit, - .sched_in = vmx_sched_in, + .sched_in = vt_sched_in, .cpu_dirty_log_size = PML_ENTITY_NUM, .update_cpu_dirty_logging = vmx_update_cpu_dirty_logging,arch/x86/kvm/vmx/tdx.c +51
-0@@ -85,6 +85,18 @@ static inline bool is_td_finalized(struct kvm_tdx *kvm_tdx)return kvm_tdx->finalized; } +static inline void tdx_disassociate_vp (struct kvm_vcpu *vcpu) +{ +/* +* Ensure tdx->cpu_list is updated is before setting vcpu->cpu to -1, +* otherwise, a different CPU can see vcpu->cpu = -1 and add the vCPU +* to its list before its deleted from this CPUs list. +*/ +smp_wmb (); + + vcpu->cpu =-1 ; +} +static void tdx_clear_page (unsigned long page) {const void *zero_page = (const void *)__va (page_to_phys (ZERO_PAGE (0 )));@@ -155,6 +167,39 @@ static void tdx_reclaim_td_page(struct tdx_td_page *page)free_page (page->va); } +static void tdx_flush_vp (void *arg) +{ +struct kvm_vcpu *vcpu = arg; +u64 err; + +/* Task migration can race with CPU offlining. */ +if (vcpu->cpu !=raw_smp_processor_id ()) +return ; + +/* +* No need to do TDH_VP_FLUSH if the vCPU hasn't been initialized. The +* list tracking still needs to be updated so that it's correct if/when +* the vCPU does get initialized. +*/ +if (is_td_vcpu_created (to_tdx (vcpu))) { + err =tdh_vp_flush (to_tdx (vcpu)->tdvpr.pa); +if (unlikely (err && err !=TDX_VCPU_NOT_ASSOCIATED )) { +if (WARN_ON_ONCE (err)) +pr_tdx_error (TDH_VP_FLUSH , err,NULL ); + } + } + +tdx_disassociate_vp (vcpu); +} + +static void tdx_flush_vp_on_cpu (struct kvm_vcpu *vcpu) +{ +if (unlikely (vcpu->cpu ==-1 )) +return ; + +smp_call_function_single (vcpu->cpu, tdx_flush_vp, vcpu,1 ); +} +static int tdx_do_tdh_phymem_cache_wb (void *param) {u64 err =0 ;@@ -425,6 +470,12 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)return ret; } +void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) +{ +if (vcpu->cpu != cpu) +tdx_flush_vp_on_cpu (vcpu); +} +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);arch/x86/kvm/vmx/x86_ops.h +1
-0@@ -143,6 +143,7 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu);void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu); +void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
arch/x86/kvm/vmx/x86_ops.h static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {} · line 173
Quoted 7 lines
arch/x86/kvm/vmx/x86_ops.h +1
-0@@ -166,6 +167,7 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}static inline fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu) {return EXIT_FASTPATH_NONE ; }static inline void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {} +static inline void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
This patch and the next one might even be squashed together.
Otherwise
RFC PATCH v5 73/104 KVM: TDX: track LP tdx vcpu run and teardown vcpus on descroing the guest TD
In reply to [email protected]From: Isaku Yamahata <[email protected]>
When shutting down the machine, (VMX or TDX) vcpus needs to be shutdown on each pcpu. Do the similar for TDX with TDX SEAMCALL APIs.
--- arch/x86/kvm/vmx/main.c | 23 +++++++++++-- arch/x86/kvm/vmx/tdx.c | 70 ++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.h | 2 ++ arch/x86/kvm/vmx/x86_ops.h | 4 +++ 4 files changed, 95 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/main.c +21 -2
@@ -13,6 +13,25 @@ static bool vt_is_vm_type_supported(unsigned long type)return type ==KVM_X86_DEFAULT_VM ||tdx_is_vm_type_supported (type); } +static int vt_hardware_enable (void ) +{ +int ret; + + ret =vmx_hardware_enable (); +if (ret) +return ret; + +tdx_hardware_enable (); +return 0 ; +} + +static void vt_hardware_disable (void ) +{ +/* Note, TDX *and* VMX need to be disabled if TDX is enabled. */ +tdx_hardware_disable (); +vmx_hardware_disable (); +} +static __init int vt_hardware_setup (void ) {int ret;
@@ -199,8 +218,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .hardware_unsetup = vt_hardware_unsetup, - .hardware_enable = vmx_hardware_enable, - .hardware_disable = vmx_hardware_disable, + .hardware_enable = vt_hardware_enable, + .hardware_disable = vt_hardware_disable, .cpu_has_accelerated_tpr = report_flexpriority, .has_emulated_msr = vmx_has_emulated_msr,
arch/x86/kvm/vmx/tdx.c +68 -2
115 lines not on this page · raw message
arch/x86/kvm/vmx/tdx.h +2 -0
@@ -81,6 +81,8 @@ struct vcpu_tdx {struct tdx_td_page tdvpr;struct tdx_td_page *tdvpx; +struct list_head cpu_list; +union tdx_exit_reason exit_reason;bool initialized;
arch/x86/kvm/vmx/x86_ops.h +4 -0
@@ -132,6 +132,8 @@ void __init tdx_pre_kvm_init(unsigned int *vcpu_size,bool tdx_is_vm_type_supported (unsigned long type);void __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);void tdx_hardware_unsetup (void ); +void tdx_hardware_enable (void ); +void tdx_hardware_disable (void );int tdx_vm_init (struct kvm *kvm);void tdx_mmu_prezap (struct kvm *kvm);
@@ -156,6 +158,8 @@ static inline void tdx_pre_kvm_init(static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }static inline void tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {}static inline void tdx_hardware_unsetup (void ) {} +static inline void tdx_hardware_enable (void ) {} +static inline void tdx_hardware_disable (void ) {}static inline int tdx_vm_init (struct kvm *kvm) {return -EOPNOTSUPP ; }static inline void tdx_mmu_prezap (struct kvm *kvm) {}
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 073/104] KVM: TDX: track LP tdx vcpu run and teardown vcpus on descroing the guest TD
Quoted 119 lines · On Fri, Mar 4, 2022 at 11:50 AM <[email protected]> wrote:
From: Isaku Yamahata <[email protected]>
When shutting down the machine, (VMX or TDX) vcpus needs to be shutdown on each pcpu. Do the similar for TDX with TDX SEAMCALL APIs.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 23 +++++++++++-- arch/x86/kvm/vmx/tdx.c | 70 ++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.h | 2 ++ arch/x86/kvm/vmx/x86_ops.h | 4 +++ 4 files changed, 95 insertions(+), 4 deletions(-)arch/x86/kvm/vmx/main.c +21
-2@@ -13,6 +13,25 @@ static bool vt_is_vm_type_supported(unsigned long type)return type ==KVM_X86_DEFAULT_VM ||tdx_is_vm_type_supported (type); } +static int vt_hardware_enable (void ) +{ +int ret; + + ret =vmx_hardware_enable (); +if (ret) +return ret; + +tdx_hardware_enable (); +return 0 ; +} + +static void vt_hardware_disable (void ) +{ +/* Note, TDX *and* VMX need to be disabled if TDX is enabled. */ +tdx_hardware_disable (); +vmx_hardware_disable (); +} +static __init int vt_hardware_setup (void ) {int ret;@@ -199,8 +218,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .hardware_unsetup = vt_hardware_unsetup, - .hardware_enable = vmx_hardware_enable, - .hardware_disable = vmx_hardware_disable, + .hardware_enable = vt_hardware_enable, + .hardware_disable = vt_hardware_disable, .cpu_has_accelerated_tpr = report_flexpriority, .has_emulated_msr = vmx_has_emulated_msr,arch/x86/kvm/vmx/tdx.c +28
-0@@ -48,6 +48,14 @@ struct tdx_capabilities tdx_caps;static DEFINE_MUTEX (tdx_lock);static struct mutex *tdx_mng_key_config_lock; +/* +* A per-CPU list of TD vCPUs associated with a given CPU. Used when a CPU +* is brought down to invoke TDH_VP_FLUSH on the approapriate TD vCPUS. +* Protected by interrupt mask. This list is manipulated in process context +* of vcpu and IPI callback. See tdx_flush_vp_on_cpu(). +*/ +static DEFINE_PER_CPU(struct list_head, associated_tdvcpus); +static u64 hkid_mask__ro_after_init ;static u8 hkid_start_pos__ro_after_init ;@@ -87,6 +95,8 @@ static inline bool is_td_finalized(struct kvm_tdx *kvm_tdx)static inline void tdx_disassociate_vp (struct kvm_vcpu *vcpu) { +list_del (&to_tdx (vcpu)->cpu_list); +/* * Ensure tdx->cpu_list is updated is before setting vcpu->cpu to -1, * otherwise, a different CPU can see vcpu->cpu = -1 and add the vCPU @@ -97,6 +107,22 @@ static inline void tdx_disassociate_vp(struct kvm_vcpu *vcpu) vcpu->cpu =-1 ; } +void tdx_hardware_enable (void ) +{ +INIT_LIST_HEAD (&per_cpu (associated_tdvcpus,raw_smp_processor_id ())); +} + +void tdx_hardware_disable (void ) +{ +int cpu =raw_smp_processor_id (); +struct list_head *tdvcpus = &per_cpu (associated_tdvcpus, cpu); +struct vcpu_tdx *tdx, *tmp; + +/* Safe variant needed as tdx_disassociate_vp() deletes the entry. */ +list_for_each_entry_safe (tdx, tmp, tdvcpus, cpu_list) +tdx_disassociate_vp (&tdx->vcpu); +} +static void tdx_clear_page (unsigned long page) {const void *zero_page = (const void *)__va (page_to_phys (ZERO_PAGE (0 )));@@ -230,9 +256,11 @@ void tdx_mmu_prezap(struct kvm *kvm)struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm);cpumask_var_t packages;bool cpumask_allocated; +struct kvm_vcpu *vcpu;u64 err;int ret;int i; +unsigned long j;if (!is_hkid_assigned (kvm_tdx))return ;
arch/x86/kvm/vmx/tdx.c void tdx_mmu_prezap(struct kvm *kvm) · line 283
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +5
-0@@ -248,6 +276,17 @@ void tdx_mmu_prezap(struct kvm *kvm)return ; } +kvm_for_each_vcpu (j, vcpu, kvm) +tdx_flush_vp_on_cpu (vcpu); + +mutex_lock (&tdx_lock); + err =tdh_mng_vpflushdone (kvm_tdx->tdr.pa);
Hi Isaku,
I am wondering about the impact of the failures on these functions. Is there any other function which recovers any failures here? When I look at the tdx_flush_vp function, it seems like it can fail due to task migration so tdx_flush_vp_on_cpu might also fail and if it fails, tdh_mng_vpflushdone returns err. Since tdx_vm_teardown does not return any error , how the VMM can free the keyid used in this TD. Will they be forever in "used state"?
Also if tdx_vm_teardown fails, the kvm_tdx->hkid is never set to -1 which will prevent tdx_vcpu_free to free and reclaim the resources allocated for the vcpu.
-Erdem
Quoted 92 lines
arch/x86/kvm/vmx/tdx.c +35
-2+mutex_unlock (&tdx_lock); +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MNG_VPFLUSHDONE , err,NULL ); +return ; + } + cpumask_allocated =zalloc_cpumask_var (&packages,GFP_KERNEL );for_each_online_cpu (i) { if (cpumask_allocated &&@@ -472,8 +511,22 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) { -if (vcpu->cpu != cpu) -tdx_flush_vp_on_cpu (vcpu); +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (vcpu->cpu == cpu) +return ; + +tdx_flush_vp_on_cpu (vcpu); + +local_irq_disable (); +/* +* Pairs with the smp_wmb() in tdx_disassociate_vp() to ensure +* vcpu->cpu is read before tdx->cpu_list. +*/ +smp_rmb (); + +list_add (&tdx->cpu_list, &per_cpu (associated_tdvcpus, cpu)); +local_irq_enable (); }void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu)@@ -522,6 +575,19 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu)tdx_reclaim_td_page (&tdx->tdvpx[i]);kfree (tdx->tdvpx);tdx_reclaim_td_page (&tdx->tdvpr); + +/* +* kvm_free_vcpus() +* -> kvm_unload_vcpu_mmu() +* +* does vcpu_load() for every vcpu after they already disassociated +* from the per cpu list when tdx_vm_teardown(). So we need to +* disassociate them again, otherwise the freed vcpu data will be +* accessed when do list_{del,add}() on associated_tdvcpus list +* later. +*/ +tdx_flush_vp_on_cpu (vcpu); +WARN_ON (vcpu->cpu !=-1 ); }void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event)arch/x86/kvm/vmx/tdx.h +2
-0@@ -81,6 +81,8 @@ struct vcpu_tdx {struct tdx_td_page tdvpr;struct tdx_td_page *tdvpx; +struct list_head cpu_list; +union tdx_exit_reason exit_reason;bool initialized;arch/x86/kvm/vmx/x86_ops.h +4
-1@@ -132,6 +132,8 @@ void __init tdx_pre_kvm_init(unsigned int *vcpu_size,bool tdx_is_vm_type_supported (unsigned long type);void __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);void tdx_hardware_unsetup (void ); +void tdx_hardware_enable (void ); +void tdx_hardware_disable (void );int tdx_vm_init (struct kvm *kvm);void tdx_mmu_prezap (struct kvm *kvm);@@ -156,6 +158,8 @@ static inline void tdx_pre_kvm_init(static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }static inline void tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {}static inline void tdx_hardware_unsetup (void ) {} +static inline void tdx_hardware_enable (void ) {} +static inline void tdx_hardware_disable (void ) {}static inline int tdx_vm_init (struct kvm *kvm) {return -EOPNOTSUPP ; }static inline void tdx_mmu_prezap (struct kvm *kvm) {} --2.25.1
Re: [RFC PATCH v5 073/104] KVM: TDX: track LP tdx vcpu run and teardown vcpus on descroing the guest TD
Quoted 68 lines · Erdem Aktas
Quoted 67 lines · On Fri, Mar 4, 2022 at 11:50 AM <[email protected]> wrote:
arch/x86/kvm/vmx/tdx.c +28
-0@@ -48,6 +48,14 @@ struct tdx_capabilities tdx_caps;static DEFINE_MUTEX (tdx_lock);static struct mutex *tdx_mng_key_config_lock; +/* +* A per-CPU list of TD vCPUs associated with a given CPU. Used when a CPU +* is brought down to invoke TDH_VP_FLUSH on the approapriate TD vCPUS. +* Protected by interrupt mask. This list is manipulated in process context +* of vcpu and IPI callback. See tdx_flush_vp_on_cpu(). +*/ +static DEFINE_PER_CPU(struct list_head, associated_tdvcpus); +static u64 hkid_mask__ro_after_init ;static u8 hkid_start_pos__ro_after_init ;@@ -87,6 +95,8 @@ static inline bool is_td_finalized(struct kvm_tdx *kvm_tdx)static inline void tdx_disassociate_vp (struct kvm_vcpu *vcpu) { +list_del (&to_tdx (vcpu)->cpu_list); +/* * Ensure tdx->cpu_list is updated is before setting vcpu->cpu to -1, * otherwise, a different CPU can see vcpu->cpu = -1 and add the vCPU @@ -97,6 +107,22 @@ static inline void tdx_disassociate_vp(struct kvm_vcpu *vcpu) vcpu->cpu =-1 ; } +void tdx_hardware_enable (void ) +{ +INIT_LIST_HEAD (&per_cpu (associated_tdvcpus,raw_smp_processor_id ())); +} + +void tdx_hardware_disable (void ) +{ +int cpu =raw_smp_processor_id (); +struct list_head *tdvcpus = &per_cpu (associated_tdvcpus, cpu); +struct vcpu_tdx *tdx, *tmp; + +/* Safe variant needed as tdx_disassociate_vp() deletes the entry. */ +list_for_each_entry_safe (tdx, tmp, tdvcpus, cpu_list) +tdx_disassociate_vp (&tdx->vcpu); +} +static void tdx_clear_page (unsigned long page) {const void *zero_page = (const void *)__va (page_to_phys (ZERO_PAGE (0 )));@@ -230,9 +256,11 @@ void tdx_mmu_prezap(struct kvm *kvm)struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm);cpumask_var_t packages;bool cpumask_allocated; +struct kvm_vcpu *vcpu;u64 err;int ret;int i; +unsigned long j;if (!is_hkid_assigned (kvm_tdx))return ;@@ -248,6 +276,17 @@ void tdx_mmu_prezap(struct kvm *kvm)return ; }
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c void tdx_mmu_prezap(struct kvm *kvm) · line 283
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +5
-0+kvm_for_each_vcpu (j, vcpu, kvm) +tdx_flush_vp_on_cpu (vcpu); + +mutex_lock (&tdx_lock); + err =tdh_mng_vpflushdone (kvm_tdx->tdr.pa);Hi Isaku,
Hi.
Quoted 10 lines
I am wondering about the impact of the failures on these functions. Is there any other function which recovers any failures here? When I look at the
tdx_flush_vpfunction, it seems like it can fail due to task migration sotdx_flush_vp_on_cpumight also fail and if it fails,tdh_mng_vpflushdonereturns err. Sincetdx_vm_teardowndoes not return any error , how the VMM can free the keyid used in this TD. Will they be forever in "used state"?
Also iftdx_vm_teardownfails, thekvm_tdx->hkidis never set to -1 which will preventtdx_vcpu_freeto free and reclaim the resources allocated for the vcpu.
mmu_prezap() is called via release callback of mmu notifier when the last mmu reference of this process is dropped. It is after all kvm vcpu fd and kvm vm fd were closed. vcpu will never run. But we still hold kvm_vcpu structures. There is no race between tdh_vp_flush()/tdh_mng_vpflushdone() here and process migration. tdh_vp_flush()/tdh_mng_vp_flushdone() should success.
The cpuid check in tdx_flush_vp() is for vcpu_load() which may race with process migration.
Anyway what if one of those TDX seamcalls fails? HKID is leaked and will be never used because there is no good way to free and use HKID safely. Such failure is due to unknown issue and probably a bug.
One mitigation is to add pr_err() when HKID leak happens. I'll add such message on next respin.
thanks,
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [RFC PATCH v5 073/104] KVM: TDX: track LP tdx vcpu run and teardown vcpus on descroing the guest TD
So the tdh_vp_flush should always succeed while vm is being torn down. Thanks Isaku for the explanation, and I think it would be great to add the error message.
-Erdem
Quoted 113 lines · Isaku Yamahata
Quoted 68 lines · Erdem Aktas
Quoted 67 lines · On Fri, Mar 4, 2022 at 11:50 AM <[email protected]> wrote:
arch/x86/kvm/vmx/tdx.c +28
-0@@ -48,6 +48,14 @@ struct tdx_capabilities tdx_caps;static DEFINE_MUTEX (tdx_lock);static struct mutex *tdx_mng_key_config_lock; +/* +* A per-CPU list of TD vCPUs associated with a given CPU. Used when a CPU +* is brought down to invoke TDH_VP_FLUSH on the approapriate TD vCPUS. +* Protected by interrupt mask. This list is manipulated in process context +* of vcpu and IPI callback. See tdx_flush_vp_on_cpu(). +*/ +static DEFINE_PER_CPU(struct list_head, associated_tdvcpus); +static u64 hkid_mask__ro_after_init ;static u8 hkid_start_pos__ro_after_init ;@@ -87,6 +95,8 @@ static inline bool is_td_finalized(struct kvm_tdx *kvm_tdx)static inline void tdx_disassociate_vp (struct kvm_vcpu *vcpu) { +list_del (&to_tdx (vcpu)->cpu_list); +/* * Ensure tdx->cpu_list is updated is before setting vcpu->cpu to -1, * otherwise, a different CPU can see vcpu->cpu = -1 and add the vCPU @@ -97,6 +107,22 @@ static inline void tdx_disassociate_vp(struct kvm_vcpu *vcpu) vcpu->cpu =-1 ; } +void tdx_hardware_enable (void ) +{ +INIT_LIST_HEAD (&per_cpu (associated_tdvcpus,raw_smp_processor_id ())); +} + +void tdx_hardware_disable (void ) +{ +int cpu =raw_smp_processor_id (); +struct list_head *tdvcpus = &per_cpu (associated_tdvcpus, cpu); +struct vcpu_tdx *tdx, *tmp; + +/* Safe variant needed as tdx_disassociate_vp() deletes the entry. */ +list_for_each_entry_safe (tdx, tmp, tdvcpus, cpu_list) +tdx_disassociate_vp (&tdx->vcpu); +} +static void tdx_clear_page (unsigned long page) {const void *zero_page = (const void *)__va (page_to_phys (ZERO_PAGE (0 )));@@ -230,9 +256,11 @@ void tdx_mmu_prezap(struct kvm *kvm)struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm);cpumask_var_t packages;bool cpumask_allocated; +struct kvm_vcpu *vcpu;u64 err;int ret;int i; +unsigned long j;if (!is_hkid_assigned (kvm_tdx))return ;@@ -248,6 +276,17 @@ void tdx_mmu_prezap(struct kvm *kvm)return ; }Quoted 7 lines
arch/x86/kvm/vmx/tdx.c void tdx_mmu_prezap(struct kvm *kvm) · line 283
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +5
-0+kvm_for_each_vcpu (j, vcpu, kvm) +tdx_flush_vp_on_cpu (vcpu); + +mutex_lock (&tdx_lock); + err =tdh_mng_vpflushdone (kvm_tdx->tdr.pa);Hi Isaku,
Hi.
Quoted 10 lines
I am wondering about the impact of the failures on these functions. Is there any other function which recovers any failures here? When I look at the
tdx_flush_vpfunction, it seems like it can fail due to task migration sotdx_flush_vp_on_cpumight also fail and if it fails,tdh_mng_vpflushdonereturns err. Sincetdx_vm_teardowndoes not return any error , how the VMM can free the keyid used in this TD. Will they be forever in "used state"?
Also iftdx_vm_teardownfails, thekvm_tdx->hkidis never set to -1 which will preventtdx_vcpu_freeto free and reclaim the resources allocated for the vcpu.
mmu_prezap()is called via release callback of mmu notifier when the last mmu reference of this process is dropped. It is after all kvm vcpu fd and kvm vm fd were closed. vcpu will never run. But we still holdkvm_vcpustructures. There is no race between tdh_vp_flush()/tdh_mng_vpflushdone() here and process migration. tdh_vp_flush()/tdh_mng_vp_flushdone() should success.The cpuid check in
tdx_flush_vp()is forvcpu_load()which may race with process migration.Anyway what if one of those TDX seamcalls fails? HKID is leaked and will be never used because there is no good way to free and use HKID safely. Such failure is due to unknown issue and probably a bug.
One mitigation is to add
pr_err()when HKID leak happens. I'll add such message on next respin.thanks,
--
Isaku Yamahata <[email protected]>
RFC PATCH v5 74/104 KVM: x86: Add a switch_db_regs flag to handle TDX's auto-switched behavior
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Add a flag, KVM_DEBUGREG_AUTO_SWITCHED_GUEST, to skip saving/restoring DRs irrespective of any other flags. TDX-SEAM unconditionally saves and restores guest DRs and reset to architectural INIT state on TD exit. So, KVM needs to save host DRs before TD enter without restoring guest DRs and restore host DRs after TD exit.
Opportunistically convert the KVM_DEBUGREG_* definitions to use BIT().
--- arch/x86/include/asm/kvm_host.h | 10 ++++++++-- arch/x86/kvm/vmx/tdx.c | 1 + arch/x86/kvm/x86.c | 3 ++- 3 files changed, 11 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +8 -2
@@ -535,8 +535,14 @@ struct kvm_pmu {struct kvm_pmu_ops ;enum { -KVM_DEBUGREG_BP_ENABLED =1 , -KVM_DEBUGREG_WONT_EXIT =2 , +KVM_DEBUGREG_BP_ENABLED =BIT (0 ), +KVM_DEBUGREG_WONT_EXIT =BIT (1 ), +KVM_DEBUGREG_RELOAD =BIT (2 ), +/* +* Guest debug registers are saved/restored by hardware on exit from +* or enter guest. KVM needn't switch them. +*/ +KVM_DEBUGREG_AUTO_SWITCH =BIT (3 ), };struct kvm_mtrr_range {
arch/x86/kvm/vmx/tdx.c +1 -0
@@ -485,6 +485,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.efer =EFER_SCE |EFER_LME |EFER_LMA |EFER_NX ; + vcpu->arch.switch_db_regs =KVM_DEBUGREG_AUTO_SWITCH ; vcpu->arch.cr0_guest_owned_bits =-1ul ; vcpu->arch.cr4_guest_owned_bits =-1ul ;
arch/x86/kvm/x86.c +2 -1
@@ -10084,7 +10084,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (vcpu->arch.guest_fpu.xfd_err)wrmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); -if (unlikely (vcpu->arch.switch_db_regs)) { +if (unlikely (vcpu->arch.switch_db_regs & ~KVM_DEBUGREG_AUTO_SWITCH )) {set_debugreg (0 ,7 );set_debugreg (vcpu->arch.eff_db[0 ],0 );set_debugreg (vcpu->arch.eff_db[1 ],1 );
@@ -10126,6 +10126,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)*/ if (unlikely (vcpu->arch.switch_db_regs &KVM_DEBUGREG_WONT_EXIT )) {WARN_ON (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ); +WARN_ON (vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH );static_call (kvm_x86_sync_dirty_debug_regs)(vcpu);kvm_update_dr0123 (vcpu);kvm_update_dr7 (vcpu);
Signature · 1 line
2.25.1
Paolo Bonzini
maintainerRe: [RFC PATCH v5 074/104] KVM: x86: Add a switch_db_regs flag to handle TDX's auto-switched behavior
Quoted 11 lines · On 3/4/22 20:49, [email protected] wrote:
vcpu->arch.efer = EFER_SCE | EFER_LME | EFER_LMA | EFER_NX;arch/x86/kvm/vmx/tdx.c +1
-0+ vcpu->arch.switch_db_regs =KVM_DEBUGREG_AUTO_SWITCH ; vcpu->arch.cr0_guest_owned_bits =-1ul ; vcpu->arch.cr4_guest_owned_bits =-1ul ;arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 10090
Quoted 8 lines
arch/x86/kvm/x86.c +1
-1@@ -10084,7 +10084,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (vcpu->arch.guest_fpu.xfd_err)wrmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); -if (unlikely (vcpu->arch.switch_db_regs)) { +if (unlikely (vcpu->arch.switch_db_regs & ~KVM_DEBUGREG_AUTO_SWITCH )) {set_debugreg (0 ,7 );set_debugreg (vcpu->arch.eff_db[0 ],0 );set_debugreg (vcpu->arch.eff_db[1 ],1 );
I'm confused. I'm probably missing something obvious, but where does KVM_DEBUGREG_AUTO_SWITCH affect the behavior of KVM? vcpu_enter_guest would still write %db0-%db3 if KVM_DEBUGREG_BP_ENABLED is set, for example.
Do you mean:
if (unlikely(vcpu->arch.switch_db_regs) &&
!unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_AUTO_SWITCH)) {?
Paolo
Quoted 8 lines
arch/x86/kvm/x86.c +1
-0@@ -10126,6 +10126,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)*/ if (unlikely (vcpu->arch.switch_db_regs &KVM_DEBUGREG_WONT_EXIT )) {WARN_ON (vcpu->guest_debug &KVM_GUESTDBG_USE_HW_BP ); +WARN_ON (vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH );static_call (kvm_x86_sync_dirty_debug_regs)(vcpu);kvm_update_dr0123 (vcpu);kvm_update_dr7 (vcpu);
Sean Christopherson
maintainerRe: [RFC PATCH v5 074/104] KVM: x86: Add a switch_db_regs flag to handle TDX's auto-switched behavior
Quoted 23 lines · Paolo Bonzini
Quoted 9 lines · On 3/4/22 20:49, [email protected] wrote:
vcpu->arch.efer = EFER_SCE | EFER_LME | EFER_LMA | EFER_NX;arch/x86/kvm/vmx/tdx.c +1
-0+ vcpu->arch.switch_db_regs =KVM_DEBUGREG_AUTO_SWITCH ; vcpu->arch.cr0_guest_owned_bits =-1ul ; vcpu->arch.cr4_guest_owned_bits =-1ul ;arch/x86/kvm/x86.c +0
-0arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 10089
Quoted 7 lines
arch/x86/kvm/x86.c +1
-1@@ -10084,7 +10084,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (vcpu->arch.guest_fpu.xfd_err)wrmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); -if (unlikely (vcpu->arch.switch_db_regs)) { +if (unlikely (vcpu->arch.switch_db_regs & ~KVM_DEBUGREG_AUTO_SWITCH )) {set_debugreg (0 ,7 );set_debugreg (vcpu->arch.eff_db[0 ],0 );set_debugreg (vcpu->arch.eff_db[1 ],1 );I'm confused. I'm probably missing something obvious, but where does
KVM_DEBUGREG_AUTO_SWITCHaffect the behavior of KVM?vcpu_enter_guestwould still write %db0-%db3 ifKVM_DEBUGREG_BP_ENABLEDis set, for example.
Quoted 6 lines
Do you mean:
if (unlikely(vcpu->arch.switch_db_regs) && !unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_AUTO_SWITCH)) {?
No, you're confused there's a crucial chunk of this patch that's missing. The host restore path was originally
if (hw_breakpoint_active() &&
!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_AUTO_SWITCHED))
hw_breakpoint_restore();The TDX module context switches the guest _and_ host debug registers. It restores the host DRs because it needs to write _something_ to hide guest state, so it might as well restore the host values. The above was an optmization to avoid rewriting all debug registers.
However, this patch was written before commit f85d40160691 ("KVM: X86: Disable hardware breakpoints unconditionally before kvm_x86->run()"). I suspect that when rebasing to newer KVM, the Intel folks discovered that DR7 isn't getting restored because the GUEST_DR7 in the SEAM VMCS will hold the zero'd value.
But rather than completely drop the flag, KVM can still avoid writing everything except DR7, i.e.
if (hw_breakpoint_active()) {
if (!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_AUTO_SWITCH))
hw_breakpoint_restore();
else
set_debugreg(__this_cpu_read(cpu_dr7), 7);
}with a comment explaining that DR7 always needs to be restored because it's preemptively cleared to play nice with the non-instrumentable sections.
RFC PATCH v5 75/104 KVM: x86: Check for pending APICv interrupt in kvm_vcpu_has_events()
In reply to [email protected]From: Sean Christopherson <[email protected]>
Return true for kvm_vcpu_has_events() if the vCPU has a pending APICv interrupt to support TDX's usage of APICv. Unlike VMX, TDX doesn't have access to vmcs.GUEST_INTR_STATUS and so can't emulate posted interrupts, i.e. needs to generate a posted interrupt and more importantly can't manually move requested interrupts into the vIRR (which it also doesn't have access to).
Because pi_has_pending_interrupt() is heavy operation which uses two atomic test bit operations and one atomic 256 bit bitmap check, introduce new callback for this check instead of reusing dy_apicv_has_pending_interrupt() callback to avoid affecting the exiting code.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/main.c | 9 +++++++++ arch/x86/kvm/x86.c | 5 ++++- 3 files changed, 14 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1491,6 +1491,7 @@ struct kvm_x86_ops {void (*start_assignment)(struct kvm *kvm);void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu); +bool (*apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu);int (*set_hv_timer)(struct kvm_vcpu *vcpu,u64 guest_deadline_tsc,bool *expired);
arch/x86/kvm/vmx/main.c +9 -0
@@ -148,6 +148,14 @@ static void vt_vcpu_load(struct kvm_vcpu *vcpu, int cpu)return vmx_vcpu_load (vcpu, cpu); } +static bool vt_apicv_has_pending_interrupt (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return pi_has_pending_interrupt (vcpu); + +return false; +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -297,6 +305,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .sync_pir_to_irr = vmx_sync_pir_to_irr, .deliver_interrupt = vmx_deliver_interrupt, .dy_apicv_has_pending_interrupt = pi_has_pending_interrupt, + .apicv_has_pending_interrupt = vt_apicv_has_pending_interrupt, .set_tss_addr = vmx_set_tss_addr, .set_identity_map_addr = vmx_set_identity_map_addr,
arch/x86/kvm/x86.c +4 -1
@@ -12111,7 +12111,10 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)if (kvm_arch_interrupt_allowed (vcpu) && (kvm_cpu_has_interrupt (vcpu) || -kvm_guest_apic_has_interrupt (vcpu))) +kvm_guest_apic_has_interrupt (vcpu) || + (vcpu->arch.apicv_active && + kvm_x86_ops.apicv_has_pending_interrupt && + kvm_x86_ops.apicv_has_pending_interrupt (vcpu))))return true;if (kvm_hv_has_stimer_pending (vcpu))
Signature · 1 line
2.25.1
Sean Christopherson
maintainerRe: [RFC PATCH v5 075/104] KVM: x86: Check for pending APICv interrupt in kvm_vcpu_has_events()
Quoted 5 lines · On Fri, Mar 04, 2022, [email protected] wrote:
From: Sean Christopherson <[email protected]>
Return true for
kvm_vcpu_has_events()if the vCPU has a pending APICv interrupt to support TDX's usage of APICv. Unlike VMX, TDX doesn't have access to vmcs.GUEST_INTR_STATUS and so can't emulate posted interrupts,
Based on the discussion in the HLT patch, this is no longer true.
Quoted 8 lines
i.e. needs to generate a posted interrupt and more importantly can't manually move requested interrupts into the vIRR (which it also doesn't have access to).
Because
pi_has_pending_interrupt()is heavy operation which uses two atomic test bit operations and one atomic 256 bit bitmap check, introduce new callback for this check instead of reusingdy_apicv_has_pending_interrupt()callback to avoid affecting the exiting code.
...
arch/x86/kvm/x86.c static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) · line 12117
Quoted 13 lines
arch/x86/kvm/x86.c +4
-1@@ -12111,7 +12111,10 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)if (kvm_arch_interrupt_allowed (vcpu) && (kvm_cpu_has_interrupt (vcpu) || -kvm_guest_apic_has_interrupt (vcpu))) +kvm_guest_apic_has_interrupt (vcpu) || + (vcpu->arch.apicv_active && + kvm_x86_ops.apicv_has_pending_interrupt && + kvm_x86_ops.apicv_has_pending_interrupt (vcpu))))
This is pretty gross (fully realizing that I wrote this patch). It's also arguably wrong as it really should be called from apic_has_interrupt_for_ppr().
1. The hook implies it is valid for APICv in general, which is misleading.
2. It's wasted effort for VMX.
3. It does a poor job of conveying _why_ TDX is different.
4. KVM is unnecessarily processing its useless "copy" of the PPR/IRR for TDX vCPUs. It's functionally not an issue unless userspace stuffs garbage into KVM's vAPIC, but it's unnecessary work.
Rather than hook this path, I would rather we tag kvm_apic has having some of its state protected. Then kvm_cpu_has_interrupt() can invoke the alternative, protected-apic-only hook when appropriate, and kvm_apic_has_interrupt() can bail immediately instead of doing useless processing of stale vAPIC state.
Note, the below moves the !apic check from tdx_vcpu_reset() to tdx_vcpu_create(). That part should be hoisted earlier in the series, there's no reason to wait until RESET to perform the check, and I suspect the WARN_ON() can be triggered by userespace.
Compile tested only...
From: Sean Christopherson <[email protected]>
Date: Fri, 8 Apr 2022 08:56:27 -0700
Subject: [PATCH] KVM: TDX: Add support for find pending IRQ in a protected
local APIC
Add flag and hook to KVM's local APIC management to support determining whether or not a TDX guest as a pending IRQ. For TDX vCPUs, the virtual APIC page is owned by the TDX module and cannot be accessed by KVM. As a result, registers that are virtualized by the CPU, e.g. PPR, cannot be read or written by KVM. To deliver interrupts for TDX guests, KVM must send an IRQ to the CPU on the posted interrupt notification vector. And to determine if TDX vCPU has a pending interrupt, KVM must check if there is an outstanding notification.
Return "no interrupt" in kvm_apic_has_interrupt() if the guest APIC is protected to short-circuit the various other flows that try to pull an IRQ out of the vAPIC, the only valid operation is querying _if_ an IRQ is pending, KVM can't do anything based on _which_ IRQ is pending.
Intentionally omit sanity checks from other flows, e.g. PPR update, so as not to degrade non-TDX guests with unecessary checks. A well-behaved KVM and userspace will never reach those flows for TDX guests, but reaching them is not fatal if something does go awry.
Note, this doesn't handle interrupts that have been delivered to the vCPU but not yet recognized by the core, i.e. interrupts that are sitting in vmcs.GUEST_INTR_STATUS. Querying that state requires a SEAMCALL and will be supported in a future patch.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/irq.c | 3 +++ arch/x86/kvm/lapic.c | 3 +++ arch/x86/kvm/lapic.h | 2 ++ arch/x86/kvm/vmx/main.c | 11 +++++++++++ arch/x86/kvm/vmx/tdx.c | 9 ++++++--- 7 files changed, 27 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -110,6 +110,7 @@ KVM_X86_OP_NULL(update_pi_irte) KVM_X86_OP_NULL(start_assignment) KVM_X86_OP_NULL(apicv_post_state_restore) KVM_X86_OP_NULL(dy_apicv_has_pending_interrupt) +KVM_X86_OP_NULL(protected_apic_has_interrupt) KVM_X86_OP_NULL(set_hv_timer) KVM_X86_OP_NULL(cancel_hv_timer) KVM_X86_OP(setup_mce)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1491,6 +1491,7 @@ struct kvm_x86_ops {void (*start_assignment)(struct kvm *kvm);void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu); +bool (*protected_apic_has_interrupt)(struct kvm_vcpu *vcpu);int (*set_hv_timer)(struct kvm_vcpu *vcpu,u64 guest_deadline_tsc,bool *expired);
arch/x86/kvm/irq.c +3 -0
@@ -96,6 +96,9 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *v)if (kvm_cpu_has_extint (v))return 1 ; +if (lapic_in_kernel (v) && v->arch.apic->guest_apic_protected) +return static_call (kvm_x86_protected_apic_has_interrupt)(v); +return kvm_apic_has_interrupt (v) !=-1 ;/* LAPIC */ }EXPORT_SYMBOL_GPL (kvm_cpu_has_interrupt);
arch/x86/kvm/lapic.c +3 -0
@@ -2503,6 +2503,9 @@ int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)if (!kvm_apic_present (vcpu))return -1 ; +if (apic->guest_apic_protected) +return -1 ; +__apic_update_ppr (apic, &ppr);return apic_has_interrupt_for_ppr (apic, ppr); }
arch/x86/kvm/lapic.h +2 -0
@@ -52,6 +52,8 @@ struct kvm_lapic {bool sw_enabled;bool irr_pending;bool lvt0_in_nmi_mode; +/* Select registers in the vAPIC cannot be read/written. */ +bool guest_apic_protected;/* Number of bits set in ISR. */ s16 isr_count;/* The highest vector set in ISR; if -1 - invalid, must scan ISR. */
arch/x86/kvm/vmx/main.c +11 -0
@@ -42,6 +42,9 @@ static __init int vt_hardware_setup(void)tdx_hardware_setup (&vt_x86_ops); +if (!enable_tdx) + vt_x86_ops.protected_apic_has_interrupt =NULL ; +if (enable_ept) {const u64 init_value = enable_tdx ?VMX_EPT_SUPPRESS_VE_BIT :0ull ; kvm_mmu_set_ept_masks(enable_ept_ad_bits,
@@ -148,6 +151,13 @@ static void vt_vcpu_load(struct kvm_vcpu *vcpu, int cpu)return vmx_vcpu_load (vcpu, cpu); } +static bool vt_protected_apic_has_interrupt (struct kvm_vcpu *vcpu) +{ +KVM_BUG_ON (!is_td_vcpu (vcpu), vcpu->kvm); + +return pi_has_pending_interrupt (vcpu); +} +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))
@@ -297,6 +307,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .sync_pir_to_irr = vmx_sync_pir_to_irr, .deliver_interrupt = vmx_deliver_interrupt, .dy_apicv_has_pending_interrupt = pi_has_pending_interrupt, + .protected_apic_has_interrupt = vt_protected_apic_has_interrupt, .set_tss_addr = vmx_set_tss_addr, .set_identity_map_addr = vmx_set_identity_map_addr,
arch/x86/kvm/vmx/tdx.c +6 -3
@@ -467,6 +467,12 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)struct vcpu_tdx *tdx =to_tdx (vcpu);int ret, i; +/* TDX only supports x2APIC, which requires an in-kernel local APIC. */ +if (!vcpu->arch.apic) +return -EINVAL ; + + vcpu->arch.apic->guest_apic_protected = true; + ret =tdx_alloc_td_page (&tdx->tdvpr);if (ret)return ret;
@@ -602,9 +608,6 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)/* TDX doesn't support INIT event. */ if (WARN_ON (init_event)) goto td_bugged; -/* TDX supports only X2APIC enabled. */ -if (WARN_ON (!vcpu->arch.apic)) - goto td_bugged;if (WARN_ON (is_td_vcpu_created (tdx))) goto td_bugged;
base-commit: f88e9fa63cbd87cda9352ee9a86a6f815744be33 --
Paolo Bonzini
maintainerRe: [RFC PATCH v5 075/104] KVM: x86: Check for pending APICv interrupt in kvm_vcpu_has_events()
Quoted 5 lines · Sean Christopherson
Quoted 3 lines
Return true for
kvm_vcpu_has_events()if the vCPU has a pending APICv interrupt to support TDX's usage of APICv. Unlike VMX, TDX doesn't have access to vmcs.GUEST_INTR_STATUS and so can't emulate posted interrupts,Based on the discussion in the HLT patch, this is no longer true.
It's still true, it only has access to RVI > PPR (which is enough to check if the vCPU is runnable).
Quoted 4 lines
Rather than hook this path, I would rather we tag
kvm_apichas having some of its state protected. Thenkvm_cpu_has_interrupt()can invoke the alternative, protected-apic-only hook when appropriate, andkvm_apic_has_interrupt()can bail immediately instead of doing useless processing of stale vAPIC state.
Agreed, this is similar to my suggestion on the HLT patch:
https://lkml.kernel.org/r/[email protected]
Paolo
RFC PATCH v5 76/104 KVM: x86: Add option to force LAPIC expiration wait
In reply to [email protected]From: Sean Christopherson <[email protected]>
Add an option to skip the IRR check-in kvm_wait_lapic_expire(). This will be used by TDX to wait if there is an outstanding notification for a TD, i.e. a virtual interrupt is being triggered via posted interrupt processing. KVM TDX doesn't emulate PI processing, i.e. there will never be a bit set in IRR/ISR, so the default behavior for APICv of querying the IRR doesn't work as intended.
--- arch/x86/kvm/lapic.c | 4 ++-- arch/x86/kvm/lapic.h | 2 +- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/lapic.c +2 -2
@@ -1620,12 +1620,12 @@ static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)__wait_lapic_expire (vcpu, tsc_deadline - guest_tsc); } -void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu) +void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu,bool force_wait) {if (lapic_in_kernel (vcpu) && vcpu->arch.apic->lapic_timer.expired_tscdeadline && vcpu->arch.apic->lapic_timer.timer_advance_ns && -lapic_timer_int_injected (vcpu)) + (force_wait ||lapic_timer_int_injected (vcpu)))__kvm_wait_lapic_expire (vcpu); }EXPORT_SYMBOL_GPL (kvm_wait_lapic_expire);
arch/x86/kvm/lapic.h +1 -1
@@ -233,7 +233,7 @@ static inline int kvm_lapic_latched_init(struct kvm_vcpu *vcpu)bool kvm_apic_pending_eoi (struct kvm_vcpu *vcpu,int vector); -void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu); +void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu,bool force_wait);void kvm_bitmap_or_dest_vcpus (struct kvm *kvm,struct kvm_lapic_irq *irq,unsigned long *vcpu_bitmap);
arch/x86/kvm/svm/svm.c +1 -1
@@ -3766,7 +3766,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)clgi ();kvm_load_guest_xsave_state (vcpu); -kvm_wait_lapic_expire (vcpu); +kvm_wait_lapic_expire (vcpu, false);/* * If this vCPU has touched SPEC_CTRL, restore the guest's value if
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -6838,7 +6838,7 @@ fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)if (enable_preemption_timer)vmx_update_hv_timer (vcpu); -kvm_wait_lapic_expire (vcpu); +kvm_wait_lapic_expire (vcpu, false);/* * If this vCPU has touched SPEC_CTRL, restore the guest's value if
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 076/104] KVM: x86: Add option to force LAPIC expiration wait
Quoted 8 lines · On 3/4/22 20:49, [email protected] wrote:
From: Sean Christopherson <[email protected]>
Add an option to skip the IRR check-in
kvm_wait_lapic_expire(). This will be used by TDX to wait if there is an outstanding notification for a TD, i.e. a virtual interrupt is being triggered via posted interrupt processing. KVM TDX doesn't emulate PI processing, i.e. there will never be a bit set in IRR/ISR, so the default behavior for APICv of querying the IRR doesn't work as intended.
Would be better to explain "doesn't work as intended" more verbosely. Otherwise,
Paolo
Quoted 67 lines
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/lapic.c | 4 ++-- arch/x86/kvm/lapic.h | 2 +- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-)arch/x86/kvm/lapic.c +2
-2@@ -1620,12 +1620,12 @@ static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)__wait_lapic_expire (vcpu, tsc_deadline - guest_tsc); } -void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu) +void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu,bool force_wait) {if (lapic_in_kernel (vcpu) && vcpu->arch.apic->lapic_timer.expired_tscdeadline && vcpu->arch.apic->lapic_timer.timer_advance_ns && -lapic_timer_int_injected (vcpu)) + (force_wait ||lapic_timer_int_injected (vcpu)))__kvm_wait_lapic_expire (vcpu); }EXPORT_SYMBOL_GPL (kvm_wait_lapic_expire);arch/x86/kvm/lapic.h +1
-1@@ -233,7 +233,7 @@ static inline int kvm_lapic_latched_init(struct kvm_vcpu *vcpu)bool kvm_apic_pending_eoi (struct kvm_vcpu *vcpu,int vector); -void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu); +void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu,bool force_wait);void kvm_bitmap_or_dest_vcpus (struct kvm *kvm,struct kvm_lapic_irq *irq,unsigned long *vcpu_bitmap);arch/x86/kvm/svm/svm.c +1
-1@@ -3766,7 +3766,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)clgi ();kvm_load_guest_xsave_state (vcpu); -kvm_wait_lapic_expire (vcpu); +kvm_wait_lapic_expire (vcpu, false);/* * If this vCPU has touched SPEC_CTRL, restore the guest's value if arch/x86/kvm/vmx/vmx.c +1
-1@@ -6838,7 +6838,7 @@ fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)if (enable_preemption_timer)vmx_update_hv_timer (vcpu); -kvm_wait_lapic_expire (vcpu); +kvm_wait_lapic_expire (vcpu, false);/* * If this vCPU has touched SPEC_CTRL, restore the guest's value if
Sean Christopherson
maintainerRe: [RFC PATCH v5 076/104] KVM: x86: Add option to force LAPIC expiration wait
In reply to [email protected]Quoted 25 lines · On Fri, Mar 04, 2022, [email protected] wrote:
From: Sean Christopherson <[email protected]>
Add an option to skip the IRR check-in
kvm_wait_lapic_expire(). This will be used by TDX to wait if there is an outstanding notification for a TD, i.e. a virtual interrupt is being triggered via posted interrupt processing. KVM TDX doesn't emulate PI processing, i.e. there will never be a bit set in IRR/ISR, so the default behavior for APICv of querying the IRR doesn't work as intended.Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/lapic.c | 4 ++-- arch/x86/kvm/lapic.h | 2 +- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-)arch/x86/kvm/lapic.c +0
-0@@ -1620,12 +1620,12 @@ static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)__wait_lapic_expire (vcpu, tsc_deadline - guest_tsc); }
arch/x86/kvm/lapic.c static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu) · line 1629
Quoted 10 lines
arch/x86/kvm/lapic.c +2
-2-void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu) +void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu,bool force_wait) {if (lapic_in_kernel (vcpu) && vcpu->arch.apic->lapic_timer.expired_tscdeadline && vcpu->arch.apic->lapic_timer.timer_advance_ns && -lapic_timer_int_injected (vcpu)) + (force_wait ||lapic_timer_int_injected (vcpu)))__kvm_wait_lapic_expire (vcpu);
If the guest_apic_protected idea works, rather than require TDX to tell the local APIC that it should wait, the common code can instead assume a timer IRQ is pending if the IRR holds garbage.
Again, compile tested only...
From: Sean Christopherson <[email protected]>
Date: Fri, 8 Apr 2022 09:24:39 -0700
Subject: [PATCH] KVM: x86: Assume timer IRQ was injected if APIC state is
proteced
If APIC state is protected, i.e. the vCPU is a TDX guest, assume a timer IRQ was injected when deciding whether or not to busy wait in the "timer advanced" path. The "real" vIRR is not readable/writable, so trying to query for a pending timer IRQ will return garbage.
Note, TDX can scour the PIR if it wants to be more precise and skip the "wait" call entirely.
--- arch/x86/kvm/lapic.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +10 -1
@@ -1531,8 +1531,17 @@ static void apic_update_lvtt(struct kvm_lapic *apic)static bool lapic_timer_int_injected (struct kvm_vcpu *vcpu) {struct kvm_lapic *apic = vcpu->arch.apic; -u32 reg =kvm_lapic_get_reg (apic,APIC_LVTT ); +u32 reg; +/* +* Assume a timer IRQ was "injected" if the APIC is protected. KVM's +* copy of the vIRR is bogus, it's the responsibility of the caller to +* precisely check whether or not a timer IRQ is pending. +*/ +if (apic->guest_apic_protected) +return true; + + reg =kvm_lapic_get_reg (apic,APIC_LVTT );if (kvm_apic_hw_enabled (apic)) {int vec = reg &APIC_VECTOR_MASK ;void *bitmap = apic->regs +APIC_ISR ;
base-commit: 33f2439cd63c84fcbc8b4cdd4eb731e83deead90 --
RFC PATCH v5 77/104 KVM: TDX: Use vcpu_to_pi_desc() uniformly in posted_intr.c
In reply to [email protected]From: Yuan Yao <[email protected]>
The helper function, vcpu_to_pi_desc(), is defined to get the posted interrupt descriptor from vcpu. There is one place that doesn't use it, but direct reference to vmx_vcpu->pi_desc. It's inconsistent.
For TDX, TDX vcpu structure will be defined and the helper function, vcpu_to_pi_desc(), will return tdx_vcpu->pi_desc for TDX case instead of vmx_vcpu->pi_desc. The direct reference to vmx_vcpu->pi_desc doesn't work for TDX.
Replace vmx_vcpu->pi_desc with the helper function, vcpu_pi_desc() for consistency and TDX.
--- arch/x86/kvm/vmx/posted_intr.c | 2 +- arch/x86/kvm/vmx/x86_ops.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/posted_intr.c +1 -1
@@ -311,7 +311,7 @@ int pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq,continue ; } - vcpu_info.pi_desc_addr =__pa (&to_vmx (vcpu)->pi_desc); + vcpu_info.pi_desc_addr =__pa (vcpu_to_pi_desc (vcpu)); vcpu_info.vector = irq.vector; trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
arch/x86/kvm/vmx/x86_ops.h +3 -0
@@ -147,6 +147,9 @@ void tdx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu);void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu); +void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu); +int tdx_deliver_posted_interrupt (struct kvm_vcpu *vcpu,int vector); +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
Signature · 1 line
2.25.1
Paolo Bonzini
maintainerRe: [RFC PATCH v5 077/104] KVM: TDX: Use vcpu_to_pi_desc() uniformly in posted_intr.c
Quoted 39 lines · On 3/4/22 20:49, [email protected] wrote:
From: Yuan Yao <[email protected]>
The helper function,
vcpu_to_pi_desc(), is defined to get the posted interrupt descriptor from vcpu. There is one place that doesn't use it, but direct reference tovmx_vcpu->pi_desc. It's inconsistent.For TDX, TDX vcpu structure will be defined and the helper function,
vcpu_to_pi_desc(), will returntdx_vcpu->pi_descfor TDX case instead ofvmx_vcpu->pi_desc. The direct reference tovmx_vcpu->pi_descdoesn't work for TDX.Replace
vmx_vcpu->pi_descwith the helper function,vcpu_pi_desc()for consistency and TDX.Signed-off-by Yuan Yao <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/posted_intr.c | 2 +- arch/x86/kvm/vmx/x86_ops.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/posted_intr.c +1
-1@@ -311,7 +311,7 @@ int pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq,continue ; } - vcpu_info.pi_desc_addr =__pa (&to_vmx (vcpu)->pi_desc); + vcpu_info.pi_desc_addr =__pa (vcpu_to_pi_desc (vcpu)); vcpu_info.vector = irq.vector; trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,arch/x86/kvm/vmx/x86_ops.h +0
-0
arch/x86/kvm/vmx/x86_ops.h void tdx_prepare_switch_to_guest(struct kvm_vcpu *vcpu); · line 155
Quoted 9 lines
arch/x86/kvm/vmx/x86_ops.h +3
-0@@ -147,6 +147,9 @@ void tdx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu);void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu); +void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu); +int tdx_deliver_posted_interrupt (struct kvm_vcpu *vcpu,int vector); +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
Applied the first hunk, the second should be squashed somewhere else.
Paolo
RFC PATCH v5 78/104 KVM: TDX: Implement interrupt injection
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX supports interrupt inject into vcpu with posted interrupt. Wire up the corresponding kvm x86 operations to posted interrupt. Move kvm_vcpu_trigger_posted_interrupt() from vmx.c to common.h to share the code.
VMX can inject interrupt by setting interrupt information field, VM_ENTRY_INTR_INFO_FIELD, of VMCS. TDX supports interrupt injection only by posted interrupt. Ignore the execution path to access VM_ENTRY_INTR_INFO_FIELD.
As cpu state is protected and apicv is enabled for the TDX guest, VMM can inject interrupt by updating posted interrupt descriptor. Treat interrupt can be injected always.
--- arch/x86/kvm/vmx/common.h | 70 +++++++++++++++++++++++++ arch/x86/kvm/vmx/main.c | 93 ++++++++++++++++++++++++++++++---- arch/x86/kvm/vmx/posted_intr.c | 6 +++ arch/x86/kvm/vmx/tdx.c | 33 ++++++++++++ arch/x86/kvm/vmx/tdx.h | 3 ++ arch/x86/kvm/vmx/vmx.c | 57 +-------------------- arch/x86/kvm/vmx/x86_ops.h | 7 ++- 7 files changed, 203 insertions(+), 66 deletions(-)
arch/x86/kvm/vmx/common.h +70 -0
@@ -4,6 +4,7 @@#include <linux/kvm_host.h> +#include "posted_intr.h" #include "mmu.h" static inline int __vmx_handle_ept_violation(struct kvm_vcpu *vcpu, gpa_t gpa,
@@ -32,4 +33,73 @@ static inline int __vmx_handle_ept_violation(struct kvm_vcpu *vcpu, gpa_t gpa, return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); } +static inline void kvm_vcpu_trigger_posted_interrupt (struct kvm_vcpu *vcpu, +int pi_vec) +{ +#ifdef CONFIG_SMP +if (vcpu->mode ==IN_GUEST_MODE ) { +/* +* The vector of interrupt to be delivered to vcpu had +* been set in PIR before this function. +* +* Following cases will be reached in this block, and +* we always send a notification event in all cases as +* explained below. +* +* Case 1: vcpu keeps in non-root mode. Sending a +* notification event posts the interrupt to vcpu. +* +* Case 2: vcpu exits to root mode and is still +* runnable. PIR will be synced to vIRR before the +* next vcpu entry. Sending a notification event in +* this case has no effect, as vcpu is not in root +* mode. +* +* Case 3: vcpu exits to root mode and is blocked. +* vcpu_block() has already synced PIR to vIRR and +* never blocks vcpu if vIRR is not cleared. Therefore, +* a blocked vcpu here does not wait for any requested +* interrupts in PIR, and sending a notification event +* which has no effect is safe here. +*/ + + apic->send_IPI_mask (get_cpu_mask (vcpu->cpu), pi_vec); +return ; + } +#endif +/* +* The vCPU isn't in the guest; wake the vCPU in case it is blocking, +* otherwise do nothing as KVM will grab the highest priority pending +* IRQ via ->sync_pir_to_irr() in vcpu_enter_guest(). +*/ +kvm_vcpu_wake_up (vcpu); +} + +/* +* Send interrupt to vcpu via posted interrupt way. +* 1. If target vcpu is running(non-root mode), send posted interrupt +* notification to vcpu and hardware will sync PIR to vIRR atomically. +* 2. If target vcpu isn't running(root mode), kick it to pick up the +* interrupt from PIR in next vmentry. +*/ +static inline void __vmx_deliver_posted_interrupt ( +struct kvm_vcpu *vcpu,struct pi_desc *pi_desc,int vector) +{ +if (pi_test_and_set_pir (vector, pi_desc)) +return ; + +/* If a previous notification has sent the IPI, nothing to do. */ +if (pi_test_and_set_on (pi_desc)) +return ; + +/* +* The implied barrier in pi_test_and_set_on() pairs with the smp_mb_*() +* after setting vcpu->mode in vcpu_enter_guest(), thus the vCPU is +* guaranteed to see PID.ON=1 and sync the PIR to IRR if triggering a +* posted interrupt "fails" because vcpu->mode != IN_GUEST_MODE. +*/ +kvm_vcpu_trigger_posted_interrupt (vcpu,POSTED_INTR_VECTOR ); +} + +#endif /* __KVM_X86_VMX_COMMON_H */
arch/x86/kvm/vmx/main.c +84 -9
127 lines not on this page · raw message
arch/x86/kvm/vmx/posted_intr.c +6 -0
@@ -7,6 +7,7 @@#include "lapic.h" #include "irq.h" #include "posted_intr.h" +#include "tdx.h" #include "trace.h" #include "vmx.h"
@@ -31,6 +32,11 @@ static DEFINE_PER_CPU(raw_spinlock_t, wakeup_vcpus_on_cpu_lock);static inline struct pi_desc *vcpu_to_pi_desc (struct kvm_vcpu *vcpu) { +#ifdef CONFIG_INTEL_TDX_HOST +if (is_td_vcpu (vcpu)) +return &(to_tdx (vcpu)->pi_desc); +#endif +return &(to_vmx (vcpu)->pi_desc); }
arch/x86/kvm/vmx/tdx.c +33 -0
@@ -7,6 +7,7 @@#include "capabilities.h" #include "x86_ops.h" +#include "common.h" #include "mmu.h" #include "tdx.h" #include "vmx.h"
@@ -494,6 +495,9 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.guest_state_protected = !(to_kvm_tdx (vcpu->kvm)->attributes &TDX_TD_ATTRIBUTE_DEBUG ); + tdx->pi_desc.nv =POSTED_INTR_VECTOR ; + tdx->pi_desc.sn =1 ; + tdx->host_state_need_save = true; tdx->host_state_need_restore = false;
@@ -514,6 +518,7 @@ void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) {struct vcpu_tdx *tdx =to_tdx (vcpu); +vmx_vcpu_pi_load (vcpu, cpu);if (vcpu->cpu == cpu)return ;
@@ -735,6 +740,12 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)trace_kvm_entry (vcpu); +if (pi_test_on (&tdx->pi_desc)) { + apic->send_IPI_self (POSTED_INTR_VECTOR ); + +kvm_wait_lapic_expire (vcpu, true); + } +tdx_vcpu_enter_exit (vcpu, tdx);tdx_user_return_update_cache ();
@@ -1008,6 +1019,24 @@ static void tdx_handle_changed_private_spte( } } +void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +pi_clear_on (&tdx->pi_desc); +memset (tdx->pi_desc.pir,0 ,sizeof (tdx->pi_desc.pir)); +} + +void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode, +int trig_mode,int vector) +{ +struct kvm_vcpu *vcpu = apic->vcpu; +struct vcpu_tdx *tdx =to_tdx (vcpu); + +/* TDX supports only posted interrupt. No lapic emulation. */ +__vmx_deliver_posted_interrupt (vcpu, &tdx->pi_desc, vector); +} +static int tdx_capabilities (struct kvm *kvm,struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;
@@ -1425,6 +1454,10 @@ int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp)return -EIO ; } +td_vmcs_write16 (tdx,POSTED_INTR_NV ,POSTED_INTR_VECTOR ); +td_vmcs_write64 (tdx,POSTED_INTR_DESC_ADDR ,__pa (&tdx->pi_desc)); +td_vmcs_setbit32 (tdx,PIN_BASED_VM_EXEC_CONTROL ,PIN_BASED_POSTED_INTR ); + tdx->initialized = true;return 0 ; }
arch/x86/kvm/vmx/tdx.h +3 -0
@@ -83,6 +83,9 @@ struct vcpu_tdx {struct list_head cpu_list; +/* Posted interrupt descriptor */ +struct pi_desc pi_desc; +union tdx_exit_reason exit_reason;bool initialized;
arch/x86/kvm/vmx/vmx.c +1 -56
69 lines not on this page · raw message
arch/x86/kvm/vmx/x86_ops.h +6 -1
@@ -148,7 +148,8 @@ void tdx_vcpu_put(struct kvm_vcpu *vcpu);void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu);void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu); -int tdx_deliver_posted_interrupt (struct kvm_vcpu *vcpu,int vector); +void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode, +int trig_mode,int vector);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -176,6 +177,10 @@ static inline void tdx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_put (struct kvm_vcpu *vcpu) {}static inline void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) {} +static inline void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu) {} +static inline void tdx_deliver_interrupt ( +struct kvm_lapic *apic,int delivery_mode,int trig_mode,int vector) {} +static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 078/104] KVM: TDX: Implement interrupt injection
Please do not duplicate code, for example:
arch/x86/kvm/vmx/tdx.c static void tdx_handle_changed_private_spte( · line 1028
Quoted 8 lines · On 3/4/22 20:49, [email protected] wrote:
arch/x86/kvm/vmx/tdx.c +7
-0+void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +pi_clear_on (&tdx->pi_desc); +memset (tdx->pi_desc.pir,0 ,sizeof (tdx->pi_desc.pir)); +}
This is the same as vmx_apicv_post_state_restore. Please write this like:
void vt_apicv_post_state_restore(struct kvm_vcpu *vcpu)
{
struct pi_desc *pi = vcpu_to_pi_desc(vcpu);
pi_clear_on(pi);
memset(pi->pir, 0, sizeof(pi->pir));
}Otherwise,
Paolo
RFC PATCH v5 79/104 KVM: TDX: Implements vcpu request_immediate_exit
In reply to [email protected]From: Isaku Yamahata <[email protected]>
Now we are able to inject interrupts into TDX vcpu, it's ready to block TDX vcpu. Wire up kvm x86 methods for blocking/unblocking vcpu for TDX. To unblock on pending events, request immediate exit methods is also needed.
--- arch/x86/kvm/vmx/main.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/main.c +9 -1
@@ -280,6 +280,14 @@ static void vt_enable_irq_window(struct kvm_vcpu *vcpu)vmx_enable_irq_window (vcpu); } +static void vt_request_immediate_exit (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return __kvm_request_immediate_exit (vcpu); + +vmx_request_immediate_exit (vcpu); +} +static int vt_mem_enc_op (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
@@ -402,7 +410,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .check_intercept = vmx_check_intercept, .handle_exit_irqoff = vmx_handle_exit_irqoff, - .request_immediate_exit = vmx_request_immediate_exit, + .request_immediate_exit = vt_request_immediate_exit, .sched_in = vt_sched_in,
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 079/104] KVM: TDX: Implements vcpu request_immediate_exit
Quoted 31 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Now we are able to inject interrupts into TDX vcpu, it's ready to block TDX vcpu. Wire up kvm x86 methods for blocking/unblocking vcpu for TDX. To unblock on pending events, request immediate exit methods is also needed.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/main.c +8
-0@@ -280,6 +280,14 @@ static void vt_enable_irq_window(struct kvm_vcpu *vcpu)vmx_enable_irq_window (vcpu); } +static void vt_request_immediate_exit (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return __kvm_request_immediate_exit (vcpu); + +vmx_request_immediate_exit (vcpu); +} +static int vt_mem_enc_op (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))
arch/x86/kvm/vmx/main.c struct kvm_x86_ops vt_x86_ops __initdata = { · line 416
Quoted 8 lines
arch/x86/kvm/vmx/main.c +1
-1@@ -402,7 +410,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .check_intercept = vmx_check_intercept, .handle_exit_irqoff = vmx_handle_exit_irqoff, - .request_immediate_exit = vmx_request_immediate_exit, + .request_immediate_exit = vt_request_immediate_exit, .sched_in = vt_sched_in,
RFC PATCH v5 80/104 KVM: TDX: Implement methods to inject NMI
In reply to [email protected]From: Isaku Yamahata <[email protected]>
TDX vcpu control structure defines one bit for pending NMI for VMM to inject NMI by setting the bit without knowing TDX vcpu NMI states. Because the vcpu state is protected, VMM can't know about NMI states of TDX vcpu. The TDX module handles actual injection and NMI states transition.
Add methods for NMI and treat NMI can be injected always.
--- arch/x86/kvm/vmx/main.c | 62 +++++++++++++++++++++++++++++++++++--- arch/x86/kvm/vmx/tdx.c | 5 +++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ 3 files changed, 64 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/main.c +57 -5
@@ -216,6 +216,58 @@ static void vt_flush_tlb_guest(struct kvm_vcpu *vcpu)vmx_flush_tlb_guest (vcpu); } +static void vt_inject_nmi (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_inject_nmi (vcpu); + +vmx_inject_nmi (vcpu); +} + +static int vt_nmi_allowed (struct kvm_vcpu *vcpu,bool for_injection) +{ +/* +* The TDX module manages NMI windows and NMI reinjection, and hides NMI +* blocking, all KVM can do is throw an NMI over the wall. +*/ +if (is_td_vcpu (vcpu)) +return true; + +return vmx_nmi_allowed (vcpu, for_injection); +} + +static bool vt_get_nmi_mask (struct kvm_vcpu *vcpu) +{ +/* +* Assume NMIs are always unmasked. KVM could query PEND_NMI and treat +* NMIs as masked if a previous NMI is still pending, but SEAMCALLs are +* expensive and the end result is unchanged as the only relevant usage +* of get_nmi_mask() is to limit the number of pending NMIs, i.e. it +* only changes whether KVM or the TDX module drops an NMI. +*/ +if (is_td_vcpu (vcpu)) +return false; + +return vmx_get_nmi_mask (vcpu); +} + +static void vt_set_nmi_mask (struct kvm_vcpu *vcpu,bool masked) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_set_nmi_mask (vcpu, masked); +} + +static void vt_enable_nmi_window (struct kvm_vcpu *vcpu) +{ +/* Refer the comment in vt_get_nmi_mask(). */ +if (is_td_vcpu (vcpu)) +return ; + +vmx_enable_nmi_window (vcpu); +} +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {
@@ -366,14 +418,14 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .get_interrupt_shadow = vt_get_interrupt_shadow, .patch_hypercall = vmx_patch_hypercall, .set_irq = vt_inject_irq, - .set_nmi = vmx_inject_nmi, + .set_nmi = vt_inject_nmi, .queue_exception = vmx_queue_exception, .cancel_injection = vt_cancel_injection, .interrupt_allowed = vt_interrupt_allowed, - .nmi_allowed = vmx_nmi_allowed, - .get_nmi_mask = vmx_get_nmi_mask, - .set_nmi_mask = vmx_set_nmi_mask, - .enable_nmi_window = vmx_enable_nmi_window, + .nmi_allowed = vt_nmi_allowed, + .get_nmi_mask = vt_get_nmi_mask, + .set_nmi_mask = vt_set_nmi_mask, + .enable_nmi_window = vt_enable_nmi_window, .enable_irq_window = vt_enable_irq_window, .update_cr8_intercept = vmx_update_cr8_intercept, .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
arch/x86/kvm/vmx/tdx.c +5 -0
@@ -763,6 +763,11 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)return EXIT_FASTPATH_NONE ; } +void tdx_inject_nmi (struct kvm_vcpu *vcpu) +{ +td_management_write8 (to_tdx (vcpu),TD_VCPU_PEND_NMI ,1 ); +} +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK );
arch/x86/kvm/vmx/x86_ops.h +2 -0
@@ -150,6 +150,7 @@ void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu);void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu);void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode,int trig_mode,int vector); +void tdx_inject_nmi (struct kvm_vcpu *vcpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
@@ -180,6 +181,7 @@ static inline void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) {}static inline void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu) {}static inline void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode,int trig_mode,int vector) {} +static inline void tdx_inject_nmi (struct kvm_vcpu *vcpu) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 080/104] KVM: TDX: Implement methods to inject NMI
Quoted 128 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX vcpu control structure defines one bit for pending NMI for VMM to inject NMI by setting the bit without knowing TDX vcpu NMI states. Because the vcpu state is protected, VMM can't know about NMI states of TDX vcpu. The TDX module handles actual injection and NMI states transition.
Add methods for NMI and treat NMI can be injected always.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 62 +++++++++++++++++++++++++++++++++++--- arch/x86/kvm/vmx/tdx.c | 5 +++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ 3 files changed, 64 insertions(+), 5 deletions(-)arch/x86/kvm/vmx/main.c +57
-5@@ -216,6 +216,58 @@ static void vt_flush_tlb_guest(struct kvm_vcpu *vcpu)vmx_flush_tlb_guest (vcpu); } +static void vt_inject_nmi (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_inject_nmi (vcpu); + +vmx_inject_nmi (vcpu); +} + +static int vt_nmi_allowed (struct kvm_vcpu *vcpu,bool for_injection) +{ +/* +* The TDX module manages NMI windows and NMI reinjection, and hides NMI +* blocking, all KVM can do is throw an NMI over the wall. +*/ +if (is_td_vcpu (vcpu)) +return true; + +return vmx_nmi_allowed (vcpu, for_injection); +} + +static bool vt_get_nmi_mask (struct kvm_vcpu *vcpu) +{ +/* +* Assume NMIs are always unmasked. KVM could query PEND_NMI and treat +* NMIs as masked if a previous NMI is still pending, but SEAMCALLs are +* expensive and the end result is unchanged as the only relevant usage +* of get_nmi_mask() is to limit the number of pending NMIs, i.e. it +* only changes whether KVM or the TDX module drops an NMI. +*/ +if (is_td_vcpu (vcpu)) +return false; + +return vmx_get_nmi_mask (vcpu); +} + +static void vt_set_nmi_mask (struct kvm_vcpu *vcpu,bool masked) +{ +if (is_td_vcpu (vcpu)) +return ; + +vmx_set_nmi_mask (vcpu, masked); +} + +static void vt_enable_nmi_window (struct kvm_vcpu *vcpu) +{ +/* Refer the comment in vt_get_nmi_mask(). */ +if (is_td_vcpu (vcpu)) +return ; + +vmx_enable_nmi_window (vcpu); +} +static void vt_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {@@ -366,14 +418,14 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .get_interrupt_shadow = vt_get_interrupt_shadow, .patch_hypercall = vmx_patch_hypercall, .set_irq = vt_inject_irq, - .set_nmi = vmx_inject_nmi, + .set_nmi = vt_inject_nmi, .queue_exception = vmx_queue_exception, .cancel_injection = vt_cancel_injection, .interrupt_allowed = vt_interrupt_allowed, - .nmi_allowed = vmx_nmi_allowed, - .get_nmi_mask = vmx_get_nmi_mask, - .set_nmi_mask = vmx_set_nmi_mask, - .enable_nmi_window = vmx_enable_nmi_window, + .nmi_allowed = vt_nmi_allowed, + .get_nmi_mask = vt_get_nmi_mask, + .set_nmi_mask = vt_set_nmi_mask, + .enable_nmi_window = vt_enable_nmi_window, .enable_irq_window = vt_enable_irq_window, .update_cr8_intercept = vmx_update_cr8_intercept, .set_virtual_apic_mode = vmx_set_virtual_apic_mode,arch/x86/kvm/vmx/tdx.c +5
-0@@ -763,6 +763,11 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)return EXIT_FASTPATH_NONE ; } +void tdx_inject_nmi (struct kvm_vcpu *vcpu) +{ +td_management_write8 (to_tdx (vcpu),TD_VCPU_PEND_NMI ,1 ); +} +void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int pgd_level) {td_vmcs_write64 (to_tdx (vcpu),SHARED_EPT_POINTER , root_hpa &PAGE_MASK );arch/x86/kvm/vmx/x86_ops.h +1
-0@@ -150,6 +150,7 @@ void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu);void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu);void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode,int trig_mode,int vector); +void tdx_inject_nmi (struct kvm_vcpu *vcpu);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);
arch/x86/kvm/vmx/x86_ops.h static inline void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) {} · line 187
Quoted 7 lines
arch/x86/kvm/vmx/x86_ops.h +1
-0@@ -180,6 +181,7 @@ static inline void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) {}static inline void tdx_apicv_post_state_restore (struct kvm_vcpu *vcpu) {}static inline void tdx_deliver_interrupt (struct kvm_lapic *apic,int delivery_mode,int trig_mode,int vector) {} +static inline void tdx_inject_nmi (struct kvm_vcpu *vcpu) {}static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; }
RFC PATCH v5 81/104 KVM: VMX: Modify NMI and INTR handlers to take intr_info as function argument
In reply to [email protected]From: Sean Christopherson <[email protected]>
TDX uses different ABI to get information about VM exit. Pass intr_info to the NMI and INTR handlers instead of pulling it from vcpu_vmx in preparation for sharing the bulk of the handlers with TDX.
When the guest TD exits to VMM, RAX holds status and exit reason, RCX holds exit qualification etc rather than the VMCS fields because VMM doesn't have access to the VMCS. The eventual code will be
VMX:
- get exit reason, intr_info, exit_qualification, and etc from VMCS
- call NMI/INTR handlers (common code)
TDX:
- get exit reason, intr_info, exit_qualification, and etc from guest registers
- call NMI/INTR handlers (common code)
--- arch/x86/kvm/vmx/vmx.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
arch/x86/kvm/vmx/vmx.c +8 -9
@@ -6442,28 +6442,27 @@ static void handle_nm_fault_irqoff(struct kvm_vcpu *vcpu)rdmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); } -static void handle_exception_nmi_irqoff (struct vcpu_vmx *vmx) +static void handle_exception_nmi_irqoff (struct kvm_vcpu *vcpu,u32 intr_info) {const unsigned long nmi_entry = (unsigned long )asm_exc_nmi_noist; -u32 intr_info =vmx_get_intr_info (&vmx->vcpu);/* if exit due to PF check for async PF */ if (is_page_fault (intr_info)) - vmx->vcpu.arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags (); + vcpu->arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags ();/* if exit due to NM, handle before interrupts are enabled */ else if (is_nm_fault (intr_info)) -handle_nm_fault_irqoff (&vmx->vcpu); +handle_nm_fault_irqoff (vcpu);/* Handle machine checks before interrupts are enabled */ else if (is_machine_check (intr_info))kvm_machine_check ();/* We need to handle NMIs before interrupts are enabled */ else if (is_nmi (intr_info)) -handle_interrupt_nmi_irqoff (&vmx->vcpu, nmi_entry); +handle_interrupt_nmi_irqoff (vcpu, nmi_entry); } -static void handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu) +static void handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu, +u32 intr_info) { -u32 intr_info =vmx_get_intr_info (vcpu);unsigned int vector = intr_info &INTR_INFO_VECTOR_MASK ;gate_desc *desc = (gate_desc *)host_idt_base + vector;
@@ -6482,9 +6481,9 @@ void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)return ;if (vmx->exit_reason.basic ==EXIT_REASON_EXTERNAL_INTERRUPT ) -handle_external_interrupt_irqoff (vcpu); +handle_external_interrupt_irqoff (vcpu,vmx_get_intr_info (vcpu));else if (vmx->exit_reason.basic ==EXIT_REASON_EXCEPTION_NMI ) -handle_exception_nmi_irqoff (vmx); +handle_exception_nmi_irqoff (vcpu,vmx_get_intr_info (vcpu)); }/*
Signature · 1 line
2.25.1
Re: [RFC PATCH v5 081/104] KVM: VMX: Modify NMI and INTR handlers to take intr_info as function argument
Quoted 66 lines · On 3/4/22 20:49, [email protected] wrote:
From: Sean Christopherson <[email protected]>
TDX uses different ABI to get information about VM exit. Pass
intr_infoto the NMI and INTR handlers instead of pulling it fromvcpu_vmxin preparation for sharing the bulk of the handlers with TDX.When the guest TD exits to VMM, RAX holds status and exit reason, RCX holds exit qualification etc rather than the VMCS fields because VMM doesn't have access to the VMCS. The eventual code will be
VMX:
- get exit reason,intr_info,exit_qualification, and etc from VMCS
- call NMI/INTR handlers (common code)TDX:
- get exit reason,intr_info,exit_qualification, and etc from guest registers
- call NMI/INTR handlers (common code)Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)arch/x86/kvm/vmx/vmx.c +6
-7@@ -6442,28 +6442,27 @@ static void handle_nm_fault_irqoff(struct kvm_vcpu *vcpu)rdmsrl (MSR_IA32_XFD_ERR , vcpu->arch.guest_fpu.xfd_err); } -static void handle_exception_nmi_irqoff (struct vcpu_vmx *vmx) +static void handle_exception_nmi_irqoff (struct kvm_vcpu *vcpu,u32 intr_info) {const unsigned long nmi_entry = (unsigned long )asm_exc_nmi_noist; -u32 intr_info =vmx_get_intr_info (&vmx->vcpu);/* if exit due to PF check for async PF */ if (is_page_fault (intr_info)) - vmx->vcpu.arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags (); + vcpu->arch.apf.host_apf_flags =kvm_read_and_reset_apf_flags ();/* if exit due to NM, handle before interrupts are enabled */ else if (is_nm_fault (intr_info)) -handle_nm_fault_irqoff (&vmx->vcpu); +handle_nm_fault_irqoff (vcpu);/* Handle machine checks before interrupts are enabled */ else if (is_machine_check (intr_info))kvm_machine_check ();/* We need to handle NMIs before interrupts are enabled */ else if (is_nmi (intr_info)) -handle_interrupt_nmi_irqoff (&vmx->vcpu, nmi_entry); +handle_interrupt_nmi_irqoff (vcpu, nmi_entry); } -static void handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu) +static void handle_external_interrupt_irqoff (struct kvm_vcpu *vcpu, +u32 intr_info) { -u32 intr_info =vmx_get_intr_info (vcpu);unsigned int vector = intr_info &INTR_INFO_VECTOR_MASK ;gate_desc *desc = (gate_desc *)host_idt_base + vector;@@ -6482,9 +6481,9 @@ void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)return ;
arch/x86/kvm/vmx/vmx.c void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu) · line 6489
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +2
-2if (vmx->exit_reason.basic ==EXIT_REASON_EXTERNAL_INTERRUPT ) -handle_external_interrupt_irqoff (vcpu); +handle_external_interrupt_irqoff (vcpu,vmx_get_intr_info (vcpu));else if (vmx->exit_reason.basic ==EXIT_REASON_EXCEPTION_NMI ) -handle_exception_nmi_irqoff (vmx); +handle_exception_nmi_irqoff (vcpu,vmx_get_intr_info (vcpu)); }/*