KVM TDX basic feature support
Subsystems (8)
Re: [RFC PATCH v5 026/104] KVM: TDX: x86: Add vm ioctl to get TDX systemwide parameters
In reply to Kai HuangQuoted 18 lines · Kai Huang
Quoted 17 lines · Xiaoyao Li
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · On 3/4/22 20:48, [email protected] wrote:
Implement a VM-scoped subcomment to get system-wide parameters. Although this is system-wide parameters not per-VM, this subcomand is VM-scoped because
- Device model needs TDX system-wide parameters after creating KVM VM.
- This subcommands requires to initialize TDX module. For lazy initialization of the TDX module, vm-scope ioctl is better.Quoted 5 lines
Since there was agreement to install the TDX module on load, please place this ioctl on the
/dev/kvmfile descriptor.At least for SEV, there were cases where the system-wide parameters are needed outside KVM, so it's better to avoid requiring a VM file descriptor.
I don't have strong preference on KVM-scope ioctl or VM-scope.
Quoted 8 lines
Quoted 5 lines
Initially, we made it KVM-scope and change it to VM-scope in this version. Yes, it returns the info from TDX module, which doesn't vary per VM. However, what if we want to return different capabilities (software controlled capabilities) per VM?
In this case, you don't return different capabilities, instead, you return the same capabilities but control the capabilities on per-VM basis.
yes, so I'm not arguing it or insisting on per-VM.
I just speak out my concern since it's user ABI.
Quoted 11 lines
Quoted 10 lines
Part of the TDX capabilities
serves likeget_supported_cpuid, making it KVM wide lacks the flexibility to return differentiated capabilities for different TDs.Quoted 4 lines
Thanks,
Paolo
Re: [RFC PATCH v5 027/104] KVM: TDX: initialize VM with TDX specific parameters
In reply to Paolo BonziniQuoted 9 lines · Paolo Bonzini
Quoted 6 lines · On 3/4/22 20:48, [email protected] wrote:
+
td_params->attributes=init_vm->attributes;
+ if (td_params->attributes&TDX_TD_ATTRIBUTE_PERFMON) { + pr_warn("TD doesn't support perfmon. KVM needs to save/restore " + "host perf registers properly.\n");
+ return-EOPNOTSUPP;
+ }Why does KVM have to hardcode this (and LBR/AMX below)? Is the level of hardware support available from
tdx_caps, for example through the CPUID configs (0xAfor this one,0xDfor LBR and AMX)?
It's wrong code. PMU is allowed.
AMX and LBR are disallowed because and the time we wrote the codes they are not supported by KVM. Now AMX should be allowed, but (arch-)LBR should be still blocked until KVM merges arch-LBR support.
Quoted 16 lines
Quoted 16 lines
+ /* PT can be exposed to TD guest regardless of KVM's XSS support */ +
guest_supported_xss&= (supported_xss|XFEATURE_MASK_PT); +td_params->xfam=guest_supported_xcr0|guest_supported_xss; + if (td_params->xfam&TDX_TD_XFAM_LBR) {
+ pr_warn("TD doesn't support LBR. KVM needs to save/restore " + "IA32_LBR_DEPTHproperly.\n");
+ return-EOPNOTSUPP;
+ }
+
+ if (td_params->xfam&TDX_TD_XFAM_AMX) {
+ pr_warn("TD doesn't support AMX. KVM needs to save/restore " + "IA32_XFD,IA32_XFD_ERRproperly.\n");
+ return-EOPNOTSUPP;
+ }
Quoted 6 lines
Quoted 4 lines
+ if (
init_vm->tsc_khz)
+guest_tsc_khz=init_vm->tsc_khz;
+ else
+guest_tsc_khz=max_tsc_khz;You can just use
kvm->arch.default_tsc_khzin the latest kvm/queue.
yes. will change it.
Re: [RFC PATCH v5 054/104] KVM: x86/tdp_mmu: Keep PRIVATE_PROHIBIT bit when zapping
In reply to [email protected]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,
Re: [RFC PATCH v5 058/104] KVM: x86/mmu: Focibly use TDP MMU for TDX
In reply to [email protected]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
Re: [RFC PATCH v5 027/104] KVM: TDX: initialize VM with TDX specific parameters
In reply to Xiaoyao LiQuoted 13 lines · Xiaoyao Li
Quoted 11 lines · Paolo Bonzini
Quoted 6 lines · On 3/4/22 20:48, [email protected] wrote:
+
td_params->attributes=init_vm->attributes;
+ if (td_params->attributes&TDX_TD_ATTRIBUTE_PERFMON) { + pr_warn("TD doesn't support perfmon. KVM needs to save/restore " + "host perf registers properly.\n");
+ return-EOPNOTSUPP;
+ }Why does KVM have to hardcode this (and LBR/AMX below)? Is the level of hardware support available from
tdx_caps, for example through the CPUID configs (0xAfor this one,0xDfor LBR and AMX)?
Quoted 5 lines
It's wrong code. PMU is allowed.
AMX and LBR are disallowed because and the time we wrote the codes they are not supported by KVM. Now AMX should be allowed, but (arch-)LBR should be still blocked until KVM merges arch-LBR support.
I think Isaku's idea is we don't support them in the first submission?
If so as I suggested, we should add a TODO in comment..
Re: [RFC PATCH v5 051/104] KVM: TDX: TDP MMU TDX support
In reply to [email protected]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
Re: [RFC PATCH v5 060/104] KVM: TDX: Create initial guest memory
In reply to [email protected]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 ;
Re: [RFC PATCH v5 042/104] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
In reply to Kai HuangQuoted 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 088/104] KVM: TDX: Add TDG.VP.VMCALL accessors to access guest vcpu registers
In reply to [email protected]Quoted 24 lines · On Fri, 2022-03-04 at 11:49 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX defines ABI for the TDX guest to call hypercall with TDG.VP.VMCALL API. To get hypercall arguments and to set return values, add accessors to guest vcpu registers.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)arch/x86/kvm/vmx/tdx.c +5
-0@@ -88,6 +88,41 @@ static __always_inline unsigned long tdexit_intr_info(struct kvm_vcpu *vcpu)return kvm_r9_read (vcpu); } +#define BUILD_TDVMCALL_ACCESSORS (param, gpr) \ +static __always_inline \ +unsigned long tdvmcall_##param##_read(struct kvm_vcpu *vcpu) \ +{ \ + return kvm_##gpr##_read(vcpu); \
arch/x86/kvm/vmx/tdx.c static __always_inline unsigned long tdexit_intr_info(struct kvm_vcpu *vcpu) · line 105
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+} \ +static __always_inline void tdvmcall_##param##_write(struct kvm_vcpu *vcpu, \ + unsigned long val) \ +{ \ + kvm_##gpr##_write(vcpu, val); \ +} +BUILD_TDVMCALL_ACCESSORS (p1, r12); +BUILD_TDVMCALL_ACCESSORS (p2, r13); +BUILD_TDVMCALL_ACCESSORS (p3, r14); +BUILD_TDVMCALL_ACCESSORS (p4, r15);
Are they needed? Do those helpers provide more information than just using kvm_{reg}_read/write()?
Quoted 23 lines
arch/x86/kvm/vmx/tdx.c +20
-0+ +static __always_inline unsigned long tdvmcall_exit_type (struct kvm_vcpu *vcpu) +{ +return kvm_r10_read (vcpu); +} +static __always_inline unsigned long tdvmcall_exit_reason (struct kvm_vcpu *vcpu) +{ +return kvm_r11_read (vcpu); +} +static __always_inline void tdvmcall_set_return_code(struct kvm_vcpu *vcpu, +long val) +{ +kvm_r10_write (vcpu, val); +} +static __always_inline void tdvmcall_set_return_val(struct kvm_vcpu *vcpu, +unsigned long val) +{ +kvm_r11_write (vcpu, val); +} +static inline bool is_td_vcpu_created (struct vcpu_tdx *tdx) {return tdx->tdvpr.added;
Re: [RFC PATCH v5 089/104] KVM: TDX: Add a placeholder for handler of TDX hypercalls (TDG.VP.VMCALL)
In reply to [email protected]Quoted 72 lines · On Fri, 2022-03-04 at 11:49 -0800, [email protected] wrote:
From: Sean Christopherson <[email protected]>
The TDX module specification defines TDG.VP.VMCALL API (TDVMCALL for short) for the guest TD to call hypercall to VMM. When the guest TD issues TDG.VP.VMCALL, the guest TD exits to VMM with a new exit reason of TDVMCALL. The arguments from the guest TD and returned values from the VMM are passed in the guest registers. The guest RCX registers indicates which registers are used.
Define the TDVMCALL exit reason, which is carved out from the VMX exit reason namespace as the TDVMCALL exit from TDX guest to TDX-SEAM is really just a VM-Exit. Add a place holder to handle TDVMCALL exit.
Co-developed-by Xiaoyao Li <[email protected]>Signed-off-by Xiaoyao Li <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/uapi/asm/vmx.h | 4 +++- arch/x86/kvm/vmx/tdx.c | 27 ++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 13 +++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-)arch/x86/include/uapi/asm/vmx.h +3
-1@@ -92,6 +92,7 @@#define EXIT_REASON_UMWAIT 67#define EXIT_REASON_TPAUSE 68#define EXIT_REASON_BUS_LOCK 74 +#define EXIT_REASON_TDCALL 77#define VMX_EXIT_REASONS \ { EXIT_REASON_EXCEPTION_NMI, "EXCEPTION_NMI" }, \@@ -154,7 +155,8 @@ { EXIT_REASON_XRSTORS, "XRSTORS" }, \ { EXIT_REASON_UMWAIT, "UMWAIT" }, \ { EXIT_REASON_TPAUSE, "TPAUSE" }, \ - { EXIT_REASON_BUS_LOCK, "BUS_LOCK" } + { EXIT_REASON_BUS_LOCK, "BUS_LOCK" }, \ + { EXIT_REASON_TDCALL, "TDCALL" } #define VMX_EXIT_REASON_FLAGS \ { VMX_EXIT_REASONS_FAILED_VMENTRY, "FAILED_VMENTRY" }arch/x86/kvm/vmx/tdx.c +7
-1@@ -780,7 +780,8 @@ 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 ); + tdx->exit_reason.full =__tdx_vcpu_run (tdx->tdvpr.pa, vcpu->arch.regs, + tdx->tdvmcall.regs_mask);guest_exit_irqoff (); }@@ -815,6 +816,11 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu)if (tdx->exit_reason.error || tdx->exit_reason.non_recoverable)return EXIT_FASTPATH_NONE ; + +if (tdx->exit_reason.basic ==EXIT_REASON_TDCALL ) + tdx->tdvmcall.rcx = vcpu->arch.regs[VCPU_REGS_RCX ]; +else + tdx->tdvmcall.rcx =0 ;return EXIT_FASTPATH_NONE ; }
arch/x86/kvm/vmx/tdx.c static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu) · line 873
Quoted 9 lines
arch/x86/kvm/vmx/tdx.c +6
-0@@ -859,6 +865,23 @@ static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu)return 0 ; } +static int handle_tdvmcall (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (tdx->tdvmcall.xmm_mask)) + goto unsupported;
Put a comment explaining this logic?
arch/x86/kvm/vmx/tdx.c static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu) · line 875
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+ +switch (tdvmcall_exit_reason (vcpu)) {
Could we rename tdxvmcall_exit_reason() to something like tdvmcall_leaf()?
Btw, why couldn't we merge previous patch to this one, so we don't have to look back and forth to figure out exactly what do those functions do?
Quoted 45 lines
arch/x86/kvm/vmx/tdx.c +11
-0+default : +break ; + } + +unsupported: +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_INVALID_OPERAND ); +return 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 );@@ -1187,6 +1210,8 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)return tdx_handle_exception (vcpu);case EXIT_REASON_EXTERNAL_INTERRUPT :return tdx_handle_external_interrupt (vcpu); +case EXIT_REASON_TDCALL : +return handle_tdvmcall (vcpu);case EXIT_REASON_EPT_VIOLATION :return tdx_handle_ept_violation (vcpu);case EXIT_REASON_EPT_MISCONFIG :arch/x86/kvm/vmx/tdx.h +13
-0@@ -86,6 +86,19 @@ struct vcpu_tdx {/* Posted interrupt descriptor */ struct pi_desc pi_desc; +union { +struct { +union { +struct { +u16 gpr_mask; +u16 xmm_mask; + }; +u32 regs_mask; + }; +u32 reserved; + }; +u64 rcx; + } tdvmcall;union tdx_exit_reason exit_reason;bool initialized;
Re: [RFC PATCH v5 101/104] KVM: TDX: Silently ignore INIT/SIPI
In reply to Paolo BonziniQuoted 18 lines · Paolo Bonzini
Quoted 8 lines · On 3/4/22 20:49, [email protected] wrote:
+ if (
kvm_init_sipi_unsupported(vcpu->kvm))
+ /*
+ * TDX doesn't support INIT. Ignore INIT event. In the + * case of SIPI, the callback of
+ *vcpu_deliver_sipi_vectorignores it.
+ */
vcpu->arch.mp_state=KVM_MP_STATE_RUNNABLE; - elseQuoted 8 lines
-
vcpu->arch.mp_state=KVM_MP_STATE_INIT_RECEIVED; + else {
+kvm_vcpu_reset(vcpu, true);
+ if (kvm_vcpu_is_bsp(apic->vcpu))
+vcpu->arch.mp_state=KVM_MP_STATE_RUNNABLE; + else
+vcpu->arch.mp_state=KVM_MP_STATE_INIT_RECEIVED; + }
Quoted 1 line
Should you check
vcpu->arch.guest_state_protectedinstead of special-casing TDX?
We cannot use vcpu->arch.guest_state_protected because TDX supports debug TD, of which the states are not protected.
At least we need another flag, I think.
Quoted 3 lines
KVM_APIC_INITis not valid for SEV-ES either, if I remember correctly.Paolo
Paolo Bonzini
maintainerRe: [RFC PATCH v5 101/104] KVM: TDX: Silently ignore INIT/SIPI
Quoted 19 lines · Xiaoyao Li
Quoted 18 lines · Paolo Bonzini
Quoted 16 lines · On 3/4/22 20:49, [email protected] wrote:
+ if (
kvm_init_sipi_unsupported(vcpu->kvm))
+ /*
+ * TDX doesn't support INIT. Ignore INIT event. In the + * case of SIPI, the callback of
+ *vcpu_deliver_sipi_vectorignores it.
+ */
vcpu->arch.mp_state=KVM_MP_STATE_RUNNABLE; - else
-vcpu->arch.mp_state=KVM_MP_STATE_INIT_RECEIVED; + else {
+kvm_vcpu_reset(vcpu, true);
+ if (kvm_vcpu_is_bsp(apic->vcpu))
+vcpu->arch.mp_state=KVM_MP_STATE_RUNNABLE; + else
+vcpu->arch.mp_state=KVM_MP_STATE_INIT_RECEIVED; + }
Quoted 5 lines
Quoted 1 line
Should you check
vcpu->arch.guest_state_protectedinstead of special-casing TDX?We cannot use
vcpu->arch.guest_state_protectedbecause TDX supports debug TD, of which the states are not protected.At least we need another flag, I think.
Let's add .deliver_init to the kvm_x86_ops then.
Paolo
Paolo Bonzini
maintainerRe: [RFC PATCH v5 083/104] KVM: x86: Split core of hypercall emulation to helper function
In reply to Sagi ShaharQuoted 37 lines · Sagi Shahar
Quoted 36 lines · On Fri, Mar 4, 2022 at 12:00 PM <[email protected]> wrote:
From: Sean Christopherson <[email protected]>
By necessity, TDX will use a different register ABI for hypercalls. Break out the core functionality so that it may be reused for TDX.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/kvm_host.h | 4 +++ arch/x86/kvm/x86.c | 54 ++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 21 deletions(-)arch/x86/include/asm/kvm_host.h +4
-0@@ -1818,6 +1818,10 @@ void kvm_request_apicv_update(struct kvm *kvm, bool activate,void __kvm_request_apicv_update (struct kvm *kvm,bool activate,unsigned long bit); +unsigned long __kvm_emulate_hypercall (struct kvm_vcpu *vcpu,unsigned long nr, +unsigned long a0,unsigned long a1, +unsigned long a2,unsigned long a3, +int op_64_bit);int kvm_emulate_hypercall (struct kvm_vcpu *vcpu); int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,arch/x86/kvm/x86.c +0
-0@@ -9090,26 +9090,15 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu)return kvm_skip_emulated_instruction (vcpu); }
Quoted 14 lines
Quoted 4 lines
arch/x86/kvm/x86.c +3
-1-int kvm_emulate_hypercall (struct kvm_vcpu *vcpu) +unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr, + unsigned long a0, unsigned long a1, + unsigned long a2, unsigned long a3,arch/x86/kvm/x86.c static int complete_hypercall_exit(struct kvm_vcpu *vcpu) · line 9102
Quoted 10 lines
arch/x86/kvm/x86.c +1
-8+ int op_64_bit) { -unsigned long nr, a0, a1, a2, a3, ret; -int op_64_bit; - -if (kvm_xen_hypercall_enabled (vcpu->kvm)) -return kvm_xen_hypercall (vcpu); - -if (kvm_hv_hypercall_enabled (vcpu)) -return kvm_hv_hypercall (vcpu);
Please keep Xen and Hyper-V hypercalls to kvm_emulate_hypercall (more on this in the reply to patch 89). __kvm_emulate_hypercall should only handle KVM hypercalls.
Quoted 4 lines
arch/x86/kvm/x86.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 9193
Quoted 4 lines
arch/x86/kvm/x86.c +4
-0+if (static_call (kvm_x86_get_cpl)(vcpu) !=0 ) { + ret = -KVM_EPERM ; + goto out; + }
Is this guaranteed by TDG.VP.VMCALL?
Paolo
Quoted 10 lines
Quoted 7 lines
arch/x86/kvm/x86.c +1
-1+ ret =__kvm_emulate_hypercall (vcpu, nr, a0, a1, a2, a3, op_64_bit); out:if (!op_64_bit) ret = (u32 )ret; --2.25.1
Sagi
Paolo Bonzini
maintainerRe: [RFC PATCH v5 089/104] KVM: TDX: Add a placeholder for handler of TDX hypercalls (TDG.VP.VMCALL)
In reply to Kai HuangQuoted 9 lines · Kai Huang
arch/x86/kvm/vmx/tdx.c static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu) · line 873
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +6
-0+static int handle_tdvmcall (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (tdx->tdvmcall.xmm_mask)) + goto unsupported;Put a comment explaining this logic?
This only seems to be necessary for Hyper-V hypercalls, which however are not supported by this series in TDX guests (because the kvm_hv_hypercall still calls kvm_*_read, likewise for Xen).
So for now this conditional can be dropped.
Since
Re: [RFC PATCH v5 091/104] KVM: TDX: Handle TDX PV CPUID hypercall
In reply to [email protected]Quoted 45 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV CPUID hypercall to the KVM backend function.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)arch/x86/kvm/vmx/tdx.c +24
-0@@ -893,6 +893,30 @@ static int tdx_emulate_vmcall(struct kvm_vcpu *vcpu)return 1 ; } +static int tdx_emulate_cpuid (struct kvm_vcpu *vcpu) +{ +u32 eax, ebx, ecx, edx; + +/* EAX and ECX for cpuid is stored in R12 and R13. */ + eax =tdvmcall_p1_read (vcpu); + ecx =tdvmcall_p2_read (vcpu); + +kvm_cpuid (vcpu, &eax, &ebx, &ecx, &edx, true); + +/* +* The returned value for CPUID (EAX, EBX, ECX, and EDX) is stored into +* R12, R13, R14, and R15. +*/ +tdvmcall_p1_write (vcpu, eax); +tdvmcall_p2_write (vcpu, ebx); +tdvmcall_p3_write (vcpu, ecx); +tdvmcall_p4_write (vcpu, edx); + +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); + +return 1 ; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
arch/x86/kvm/vmx/tdx.c static int handle_tdvmcall(struct kvm_vcpu *vcpu) · line 936
Quoted 9 lines
arch/x86/kvm/vmx/tdx.c +3
-0@@ -904,6 +928,9 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)return tdx_emulate_vmcall (vcpu);switch (tdvmcall_exit_reason (vcpu)) { +case EXIT_REASON_CPUID : +return tdx_emulate_cpuid (vcpu); +default :break ; }
but I don't think tdvmcall_*_{read,write} add much.
Paolo
Paolo Bonzini
maintainerRe: [RFC PATCH v5 047/104] KVM: x86/mmu: add a private pointer to struct kvm_mmu_page
In reply to Kai HuangQuoted 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
Paolo Bonzini
maintainerRe: [RFC PATCH v5 045/104] KVM: x86/tdp_mmu: make REMOVED_SPTE include shadow_initial value
In reply to Sean ChristophersonQuoted 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
Paolo Bonzini
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
In reply to [email protected]arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 922
Quoted 1 line · On 3/4/22 20:49, [email protected] wrote:
arch/x86/kvm/vmx/tdx.c +1
-0+bool interrupt_disabled =tdvmcall_p1_read (vcpu);
Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 931
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +3
-0+* Virtual interrupt can arrive after TDG.VM.VMCALL<HLT> during +* the TDX module executing. On the other hand, KVM doesn't +* know if vcpu was executing in the guest TD or the TDX module.
I don't understand this; why isn't it enough to check PI.ON or something like that as part of HLT emulation?
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 954
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+ details.full =td_state_non_arch_read64 ( +to_tdx (vcpu),TD_VCPU_STATE_DETAILS_NON_ARCH );
TDX documentation says "the meaning of the field may change with Intel TDX module version", where is this field documented? I cannot find any "other guest state" fields in the TDX documentation.
Paolo
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +5
-0+if (details.vmxip) +return 1 ; + } + +return kvm_emulate_halt_noskip (vcpu);
Sean Christopherson
maintainerRe: [RFC PATCH v5 089/104] KVM: TDX: Add a placeholder for handler of TDX hypercalls (TDG.VP.VMCALL)
In reply to Paolo BonziniQuoted 10 lines · Paolo Bonzini
Quoted 8 lines · Kai Huang
arch/x86/kvm/vmx/tdx.c static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu) · line 873
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +6
-0+static int handle_tdvmcall (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (tdx->tdvmcall.xmm_mask)) + goto unsupported;Put a comment explaining this logic?
Quoted 5 lines
This only seems to be necessary for Hyper-V hypercalls, which however are not supported by this series in TDX guests (because the
kvm_hv_hypercallstill calls kvm_*_read, likewise for Xen).So for now this conditional can be dropped.
I'd prefer to keep the sanity check, it's a cheap and easy way to detect a clear cut guest bug. E.g. KVM would be within its rights to write garbage the XMM registers in this case. Even though KVM isn't to be trusted, KVM can still be nice to the guest.
Sean Christopherson
maintainerRe: [RFC PATCH v5 091/104] KVM: TDX: Handle TDX PV CPUID hypercall
In reply to Paolo BonziniQuoted 42 lines · Paolo Bonzini
Quoted 41 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV CPUID hypercall to the KVM backend function.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)arch/x86/kvm/vmx/tdx.c +24
-0@@ -893,6 +893,30 @@ static int tdx_emulate_vmcall(struct kvm_vcpu *vcpu)return 1 ; } +static int tdx_emulate_cpuid (struct kvm_vcpu *vcpu) +{ +u32 eax, ebx, ecx, edx; + +/* EAX and ECX for cpuid is stored in R12 and R13. */ + eax =tdvmcall_p1_read (vcpu); + ecx =tdvmcall_p2_read (vcpu); + +kvm_cpuid (vcpu, &eax, &ebx, &ecx, &edx, true); + +/* +* The returned value for CPUID (EAX, EBX, ECX, and EDX) is stored into +* R12, R13, R14, and R15. +*/ +tdvmcall_p1_write (vcpu, eax); +tdvmcall_p2_write (vcpu, ebx); +tdvmcall_p3_write (vcpu, ecx); +tdvmcall_p4_write (vcpu, edx); + +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); + +return 1 ; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu)
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c static int handle_tdvmcall(struct kvm_vcpu *vcpu) · line 932
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +3
-0{struct vcpu_tdx *tdx =to_tdx (vcpu);@@ -904,6 +928,9 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)return tdx_emulate_vmcall (vcpu);switch (tdvmcall_exit_reason (vcpu)) { +case EXIT_REASON_CPUID : +return tdx_emulate_cpuid (vcpu); +
Spurious whitespace that gets deleted by the HLT patch.
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c static int handle_tdvmcall(struct kvm_vcpu *vcpu) · line 936
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +0
-0default :break ; }Reviewed-by Paolo Bonzini <[email protected]>but I don't think tdvmcall_*_{read,write} add much.
They provided a lot more value when the ABI was still in flux, but I still like having them. That said, either the comments about R12..R15 need to go, or the wrappers need to go. Having both is confusing.
Sean Christopherson
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
In reply to [email protected]Quoted 10 lines · On Fri, Mar 04, 2022, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV HLT hypercall to the KVM backend function.
When the guest issues HLT, the hypercall instruction can be the right after CLI instruction. Atomically unmask virtual interrupt and issue HLT hypercall. The virtual interrupts can arrive right after CLI instruction before switching back to VMM. In such a case, the VMM should return to the guest without losing the interrupt. Check if interrupts arrived before the TDX module switching to VMM. And return to the guest in such cases.
Pretty sure you mean STI, not CLI.
Sean Christopherson
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
In reply to Paolo BonziniQuoted 11 lines · Paolo Bonzini
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 922
Quoted 1 line · On 3/4/22 20:49, [email protected] wrote:
arch/x86/kvm/vmx/tdx.c +1
-0+bool interrupt_disabled =tdvmcall_p1_read (vcpu);Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 931
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +3
-0+* Virtual interrupt can arrive after TDG.VM.VMCALL<HLT> during +* the TDX module executing. On the other hand, KVM doesn't +* know if vcpu was executing in the guest TD or the TDX module. I don't understand this; why isn't it enough to check PI.ON or something like that as part of HLT emulation?
Ooh, I think I remember what this is. This is for the case where the virtual interrupt is recognized, i.e. set in vmcs.RVI, between the STI and "HLT". KVM doesn't have access to RVI and the interrupt is no longer in the PID (because it was "recognized". It doesn't get delivered in the guest because the TDCALL completes before interrupts are enabled.
I lobbied to get this fixed in the TDX module by immediately resuming the guest in this case, but obviously that was unsuccessful.
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 954
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+ details.full =td_state_non_arch_read64 ( +to_tdx (vcpu),TD_VCPU_STATE_DETAILS_NON_ARCH );TDX documentation says "the meaning of the field may change with Intel TDX module version", where is this field documented? I cannot find any "other guest state" fields in the TDX documentation.
IMO we should put a stake in the ground and refuse to accept code that consumes "non-architectural" state. It's all software, having non-architectural APIs is completely ridiculous.
Paolo Bonzini
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
Quoted 13 lines · Sean Christopherson
Quoted 11 lines · Paolo Bonzini
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 922
Quoted 1 line · On 3/4/22 20:49, [email protected] wrote:
arch/x86/kvm/vmx/tdx.c +1
-0+bool interrupt_disabled =tdvmcall_p1_read (vcpu);Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 931
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +3
-0+* Virtual interrupt can arrive after TDG.VM.VMCALL<HLT> during +* the TDX module executing. On the other hand, KVM doesn't +* know if vcpu was executing in the guest TD or the TDX module. I don't understand this; why isn't it enough to check PI.ON or something like that as part of HLT emulation?
Quoted 8 lines
Ooh, I think I remember what this is. This is for the case where the virtual interrupt is recognized, i.e. set in vmcs.RVI, between the STI and "HLT". KVM doesn't have access to RVI and the interrupt is no longer in the PID (because it was "recognized". It doesn't get delivered in the guest because the TDCALL completes before interrupts are enabled.
I lobbied to get this fixed in the TDX module by immediately resuming the guest in this case, but obviously that was unsuccessful.
So the TDX module sets RVI while in an STI interrupt shadow. So far so good. Then:
- it receives the HLT TDCALL from the guest. The interrupt shadow at this point is gone.
- it knows that there is an interrupt that can be delivered (RVI > PPR && EFLAGS.IF=1, the other conditions of 29.2.2 don't matter)
- it forwards the HLT TDCALL nevertheless, to a clueless hypervisor that has no way to glean either RVI or PPR?
It's absurd that this be treated as anything but a bug.
Until that is fixed, KVM needs to do something like:
- every time a bit is set in PID.PIR, set tdx->buggy_hlt_workaround = 1
- every time TDG.VP.VMCALL<HLT> is received, xchg(&tdx->buggy_hlt_workaround, 0) and return immediately to the guest if it is 1.
Basically an internal version of PID.ON.
Quoted 10 lines
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 954
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+ details.full =td_state_non_arch_read64 ( +to_tdx (vcpu),TD_VCPU_STATE_DETAILS_NON_ARCH );TDX documentation says "the meaning of the field may change with Intel TDX module version", where is this field documented? I cannot find any "other guest state" fields in the TDX documentation.
IMO we should put a stake in the ground and refuse to accept code that consumes "non-architectural" state. It's all software, having non-architectural APIs is completely ridiculous.
Having them is fine, *using* them to work around undocumented bugs is the ridiculous part.
You didn't answer the other question, which is "Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?" though... Should I be worried? :)
Paolo
Sean Christopherson
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
Quoted 29 lines · Paolo Bonzini
Quoted 13 lines · Sean Christopherson
Quoted 11 lines · Paolo Bonzini
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 922
Quoted 1 line · On 3/4/22 20:49, [email protected] wrote:
arch/x86/kvm/vmx/tdx.c +1
-0+bool interrupt_disabled =tdvmcall_p1_read (vcpu);Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) · line 931
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +3
-0+* Virtual interrupt can arrive after TDG.VM.VMCALL<HLT> during +* the TDX module executing. On the other hand, KVM doesn't +* know if vcpu was executing in the guest TD or the TDX module. I don't understand this; why isn't it enough to check PI.ON or something like that as part of HLT emulation?
Quoted 8 lines
Ooh, I think I remember what this is. This is for the case where the virtual interrupt is recognized, i.e. set in vmcs.RVI, between the STI and "HLT". KVM doesn't have access to RVI and the interrupt is no longer in the PID (because it was "recognized". It doesn't get delivered in the guest because the TDCALL completes before interrupts are enabled.
I lobbied to get this fixed in the TDX module by immediately resuming the guest in this case, but obviously that was unsuccessful.
So the TDX module sets RVI while in an STI interrupt shadow. So far so good. Then:
- it receives the HLT TDCALL from the guest. The interrupt shadow at this point is gone.
Quoted 7 lines
- it knows that there is an interrupt that can be delivered (RVI > PPR && EFLAGS.IF=1, the other conditions of 29.2.2 don't matter)
- it forwards the HLT TDCALL nevertheless, to a clueless hypervisor that has no way to glean either RVI or PPR?
It's absurd that this be treated as anything but a bug.
That's what I said! :-)
Paolo Bonzini
maintainerRe: [RFC PATCH v5 091/104] KVM: TDX: Handle TDX PV CPUID hypercall
In reply to Sean ChristophersonQuoted 7 lines · Sean Christopherson
Quoted 3 lines
Reviewed-by Paolo Bonzini<[email protected]>but I don't think tdvmcall_*_{read,write} add much.
They provided a lot more value when the ABI was still in flux, but I still like having them. That said, either the comments about R12..R15 need to go, or the wrappers need to go. Having both is confusing.
Fair enough, let's keep them but rename them a0..a3 for consistency with kvm_emulate_hypercall.
Paolo
Paolo Bonzini
maintainerRe: [RFC PATCH v5 089/104] KVM: TDX: Add a placeholder for handler of TDX hypercalls (TDG.VP.VMCALL)
In reply to Sean ChristophersonQuoted 11 lines · Sean Christopherson
Quoted 10 lines · Paolo Bonzini
Quoted 8 lines · Kai Huang
arch/x86/kvm/vmx/tdx.c static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu) · line 873
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +6
-0+static int handle_tdvmcall (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (tdx->tdvmcall.xmm_mask)) + goto unsupported;Put a comment explaining this logic?
Quoted 8 lines
Quoted 5 lines
This only seems to be necessary for Hyper-V hypercalls, which however are not supported by this series in TDX guests (because the
kvm_hv_hypercallstill calls kvm_*_read, likewise for Xen).So for now this conditional can be dropped.
I'd prefer to keep the sanity check, it's a cheap and easy way to detect a clear cut guest bug.
I don't think it's necessarily a guest bug, just silly but valid behavior.
Paolo
E.g. KVM would be within its rights to write garbage the XMM
Quoted 2 lines
registers in this case. Even though KVM isn't to be trusted, KVM can still be nice to the guest.
Sean Christopherson
maintainerRe: [RFC PATCH v5 089/104] KVM: TDX: Add a placeholder for handler of TDX hypercalls (TDG.VP.VMCALL)
Quoted 16 lines · Paolo Bonzini
Quoted 15 lines · Sean Christopherson
Quoted 14 lines · Paolo Bonzini
Quoted 8 lines · Kai Huang
arch/x86/kvm/vmx/tdx.c static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu) · line 873
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +6
-0+static int handle_tdvmcall (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (tdx->tdvmcall.xmm_mask)) + goto unsupported;Put a comment explaining this logic?
This only seems to be necessary for Hyper-V hypercalls, which however are not supported by this series in TDX guests (because the
kvm_hv_hypercallstill calls kvm_*_read, likewise for Xen).
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
So for now this conditional can be dropped.
I'd prefer to keep the sanity check, it's a cheap and easy way to detect a clear cut guest bug.
I don't think it's necessarily a guest bug, just silly but valid behavior.
It's a bug from a security perspective given that letting the host unnecessarily manipulate register state is an exploit waiting to happen.
Though for KVM to reject the TDVMCALLs, the GHCI should really be updated to state that exposing more state than is required _may_ be considered invalid ("may" so that KVM isn't required to check the mask on every exit, which IMO is beyond tedious).
Re: [RFC PATCH v5 047/104] KVM: x86/mmu: add a private pointer to struct kvm_mmu_page
In reply to Paolo BonziniQuoted 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 089/104] KVM: TDX: Add a placeholder for handler of TDX hypercalls (TDG.VP.VMCALL)
In reply to Sean ChristophersonQuoted 19 lines · Sean Christopherson
Quoted 18 lines · Paolo Bonzini
Quoted 17 lines · Sean Christopherson
Quoted 15 lines · Paolo Bonzini
Quoted 8 lines · Kai Huang
arch/x86/kvm/vmx/tdx.c static int tdx_handle_triple_fault(struct kvm_vcpu *vcpu) · line 873
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +6
-0+static int handle_tdvmcall (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (unlikely (tdx->tdvmcall.xmm_mask)) + goto unsupported;Put a comment explaining this logic?
This only seems to be necessary for Hyper-V hypercalls, which however are not supported by this series in TDX guests (because the
kvm_hv_hypercallstill calls kvm_*_read, likewise for Xen).So for now this conditional can be dropped.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
I'd prefer to keep the sanity check, it's a cheap and easy way to detect a clear cut guest bug.
I don't think it's necessarily a guest bug, just silly but valid behavior.
It's a bug from a security perspective given that letting the host unnecessarily manipulate register state is an exploit waiting to happen.
Security perspective from guest's or host's respective? It's guest's responsibility to make sure itself doesn't expose unnecessarily security holes. In this particular case, if guest does, then it's guest's business, but I don't see how it can compromise host's security, or other VM's security?
As Paolo said it's a valid operation from guest, perhaps an alternative is KVM can unconditionally clear XMM registers, instead of rejecting this VMCALL.
Quoted 4 lines
Though for KVM to reject the TDVMCALLs, the GHCI should really be updated to state that exposing more state than is required _may_ be considered invalid ("may" so that KVM isn't required to check the mask on every exit, which IMO is beyond tedious).
Independent from this issue, I guess it's good for GHCI to have this anyway.
Re: [RFC PATCH v5 047/104] KVM: x86/mmu: add a private pointer to struct kvm_mmu_page
In reply to Paolo BonziniQuoted 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
Re: [RFC PATCH v5 020/104] KVM: TDX: allocate per-package mutex
In reply to Paolo BonziniQuoted 25 lines · Paolo Bonzini
Quoted 6 lines · On 3/4/22 20:48, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Several TDX SEAMCALLs are per-package scope (concretely per memory controller) and they need to be serialized per-package. Allocate mutex for it.
Quoted 6 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 8 +++++++- arch/x86/kvm/vmx/tdx.c | 18 ++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ 3 files changed, 27 insertions(+), 1 deletion(-)Please define here the lock/unlock functions as well:
static inline int
tdx_mng_key_lock(void){ int cpu = get_cpu(); cur_pkg = topology_physical_package_id(cpu); mutex_lock(&tdx_mng_key_config_lock[cur_pkg]); return cur_pkg; }
Quoted 5 lines
static inline void
tdx_mng_key_unlock(int cur_pkg){ mutex_unlock(&tdx_mng_key_config_lock[cur_pkg]); put_cpu(); }
Sure, will do in the next respoin.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [RFC PATCH v5 024/104] KVM: TDX: create/destroy VM structure
In reply to Paolo BonziniQuoted 13 lines · Paolo Bonzini
Quoted 13 lines
arch/x86/kvm/vmx/main.c +1
-1@@ -39,12 +39,24 @@ static int vt_vm_init(struct kvm *kvm) ret =tdx_module_setup ();if (ret)return ret; -return -EOPNOTSUPP ;/* Not ready to create guest TD yet. */ +return tdx_vm_init (kvm); }return vmx_vm_init (kvm); }
Quoted 8 lines
arch/x86/kvm/vmx/main.c static int vt_vm_init(struct kvm *kvm) · line 52
Quoted 5 lines
arch/x86/kvm/vmx/main.c +5
-0+static void vt_mmu_prezap (struct kvm *kvm) +{ +if (is_td (kvm)) +return tdx_mmu_prezap (kvm); +}Please rename the function to explain what it does, for example
tdx_mmu_release_hkid.
In the patch 021/104, you suggested flush_shadow_all_private(). Which do you prefer? flush_shadow_all_private or tdx_mmu_release_hkid.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [RFC PATCH v5 025/104] KVM: TDX: Add place holder for TDX VM specific mem_enc_op ioctl
In reply to Paolo BonziniQuoted 49 lines · Paolo Bonzini
Quoted 34 lines · On 3/4/22 20:48, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Add a place holder function for TDX specific VM-scoped ioctl as
mem_enc_op. TDX specific sub-commands will be added to retrieve/pass TDX specific parameters.
KVM_MEMORY_ENCRYPT_OPwas introduced for VM-scoped operations specific for guest state-protected VM. It defined subcommands for technology-specific operations underKVM_MEMORY_ENCRYPT_OP. Despite its name, the subcommands are not limited to memory encryption, but various technology-specific operations are defined. It's natural to repurposeKVM_MEMORY_ENCRYPT_OPfor TDX specific operations and define subcommands.TDX requires VM-scoped, and VCPU-scoped TDX-specific operations for device model, for example, qemu. Getting system-wide parameters, TDX-specific VM initialization, and TDX-specific vCPU initialization. Which requires KVM vCPU-scoped operations in addition to the existing VM-scoped operations.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/uapi/asm/kvm.h | 11 +++++++++++ arch/x86/kvm/vmx/main.c | 10 ++++++++++ arch/x86/kvm/vmx/tdx.c | 24 ++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 4 ++++ tools/arch/x86/include/uapi/asm/kvm.h | 11 +++++++++++ 5 files changed, 60 insertions(+)arch/x86/include/uapi/asm/kvm.h +0
-0@@ -528,4 +528,15 @@ struct kvm_pmu_event_filter {#define KVM_X86_DEFAULT_VM 0#define KVM_X86_TDX_VM 1arch/x86/include/uapi/asm/kvm.h struct kvm_pmu_event_filter { · line 539
Quoted 10 lines
arch/x86/include/uapi/asm/kvm.h +10
-0+/* Trust Domain eXtension sub-ioctl() commands. */ +enum kvm_tdx_cmd_id { +KVM_TDX_CMD_NR_MAX , +}; + +struct kvm_tdx_cmd { +__u32 id; +__u32 metadata; +__u64 data; +};Please include some initial documentation here already, for example it is not clear what "metadata" is.
Quoted 7 lines
Also please add
u32 error; u32 unused;for two reasons: 1) consistency with
kvm_sev_cmd2) error codes should be returned to userspace and not just sent throughpr_tdx_error.
Sure.
For now metadata is only used to specify flags specific to id. So I'll rename it to flags.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [RFC PATCH v5 026/104] KVM: TDX: x86: Add vm ioctl to get TDX systemwide parameters
In reply to Xiaoyao LiQuoted 25 lines · Xiaoyao Li
Quoted 24 lines · Kai Huang
Quoted 22 lines · Xiaoyao Li
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · On 3/4/22 20:48, [email protected] wrote:
Implement a VM-scoped subcomment to get system-wide parameters. Although this is system-wide parameters not per-VM, this subcomand is VM-scoped because
- Device model needs TDX system-wide parameters after creating KVM VM.
- This subcommands requires to initialize TDX module. For lazy initialization of the TDX module, vm-scope ioctl is better.Quoted 5 lines
Since there was agreement to install the TDX module on load, please place this ioctl on the
/dev/kvmfile descriptor.At least for SEV, there were cases where the system-wide parameters are needed outside KVM, so it's better to avoid requiring a VM file descriptor.
I don't have strong preference on KVM-scope ioctl or VM-scope.
Initially, we made it KVM-scope and change it to VM-scope in this version. Yes, it returns the info from TDX module, which doesn't vary per VM. However, what if we want to return different capabilities (software controlled capabilities) per VM?
Quoted 6 lines
Quoted 2 lines
In this case, you don't return different capabilities, instead, you return the same capabilities but control the capabilities on per-VM basis.
yes, so I'm not arguing it or insisting on per-VM.
I just speak out my concern since it's user ABI.
The reason why I made this API to VM-scope API is to reduce the number of patch given qemu usage. Now Paolo requested it, I'll change it KVM-scope API.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [RFC PATCH v5 027/104] KVM: TDX: initialize VM with TDX specific parameters
In reply to Kai HuangQuoted 25 lines · Kai Huang
Quoted 13 lines
arch/x86/include/uapi/asm/kvm.h +1
-0@@ -531,6 +531,7 @@ struct kvm_pmu_event_filter {/* Trust Domain eXtension sub-ioctl() commands. */ enum kvm_tdx_cmd_id {KVM_TDX_CAPABILITIES =0 , +KVM_TDX_INIT_VM ,KVM_TDX_CMD_NR_MAX , };arch/x86/include/uapi/asm/kvm.h struct kvm_tdx_capabilities { · line 569
Quoted 8 lines
arch/x86/include/uapi/asm/kvm.h +5
-0@@ -561,4 +562,15 @@ struct kvm_tdx_capabilities {struct kvm_tdx_cpuid_config cpuid_configs[0 ]; }; +struct kvm_tdx_init_vm { +__u32 max_vcpus; +__u32 tsc_khz; +__u64 attributes; +__u64 cpuid;Is it better to append all CPUIDs directly into this structure, perhaps at end of this structure, to make it more consistent with
TD_PARAMS?
Quoted 2 lines
Also, I think somewhere in commit message or comments we should explain why CPUIDs are passed here (why existing
KVM_SET_CUPID2is not sufficient).
Ok, let's change the data structure to match more with TD_PARAMS.
Quoted 46 lines
Quoted 46 lines
arch/x86/kvm/vmx/tdx.c +38
-0@@ -387,6 +387,203 @@ static int tdx_capabilities(struct kvm *kvm, struct kvm_tdx_cmd *cmd)return 0 ; } +static struct kvm_cpuid_entry2 *tdx_find_cpuid_entry (struct kvm_tdx *kvm_tdx, +u32 function,u32 index) +{ +struct kvm_cpuid_entry2 *e; +int i; + +for (i =0 ; i < kvm_tdx->cpuid_nent; i++) { + e = &kvm_tdx->cpuid_entries[i]; + +if (e->function == function && (e->index == index || + !(e->flags &KVM_CPUID_FLAG_SIGNIFCANT_INDEX ))) +return e; + } +return NULL ; +} + +static int setup_tdparams (struct kvm *kvm,struct td_params *td_params, +struct kvm_tdx_init_vm *init_vm) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +struct tdx_cpuid_config *config; +struct kvm_cpuid_entry2 *entry; +struct tdx_cpuid_value *value; +u64 guest_supported_xcr0; +u64 guest_supported_xss; +u32 guest_tsc_khz; +int max_pa; +int i; + +/* init_vm->reserved must be zero */ +if (find_first_bit ((unsigned long *)init_vm->reserved, +sizeof (init_vm->reserved) *8 ) != +sizeof (init_vm->reserved) *8 ) +return -EINVAL ; + + td_params->max_vcpus = init_vm->max_vcpus; + + td_params->attributes = init_vm->attributes;
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c static int tdx_capabilities(struct kvm *kvm, struct kvm_tdx_cmd *cmd) · line 432
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +5
-0+if (td_params->attributes &TDX_TD_ATTRIBUTE_PERFMON ) { +pr_warn ("TD doesn't support perfmon. KVM needs to save/restore " +"host perf registers properly.\n" ); +return -EOPNOTSUPP ; + }PERFMON can be supported but it's not support in this series, so perhaps add a comment to explain it's a TODO?
Yes, good idea. Will do.
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +6
-0+ max_pa =36 ; + entry =tdx_find_cpuid_entry (kvm_tdx,0x80000008 ,0 ); +if (entry) + max_pa = entry->eax &0xff ; + + td_params->eptp_controls =VMX_EPTP_MT_WB ;
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c static int tdx_capabilities(struct kvm *kvm, struct kvm_tdx_cmd *cmd) · line 465
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +6
-0+if (cpu_has_vmx_ept_5levels () && max_pa >48 ) { + td_params->eptp_controls |=VMX_EPTP_PWL_5 ; + td_params->exec_controls |=TDX_EXEC_CONTROL_MAX_GPAW ; + }else { + td_params->eptp_controls |=VMX_EPTP_PWL_4 ; + }Not quite sure, but could we support >48 GPA with 4-level EPT?
No.
"5-level paging and 5-level EPT"
section 4.1 4-level EPT
"4-level EPT is limited to translating 48-bit guest-physical addresses."
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [RFC PATCH v5 027/104] KVM: TDX: initialize VM with TDX specific parameters
In reply to Kai HuangQuoted 16 lines · Kai Huang
Quoted 15 lines · Xiaoyao Li
Quoted 11 lines · Paolo Bonzini
Quoted 6 lines · On 3/4/22 20:48, [email protected] wrote:
+
td_params->attributes=init_vm->attributes;
+ if (td_params->attributes&TDX_TD_ATTRIBUTE_PERFMON) { + pr_warn("TD doesn't support perfmon. KVM needs to save/restore " + "host perf registers properly.\n");
+ return-EOPNOTSUPP;
+ }Why does KVM have to hardcode this (and LBR/AMX below)? Is the level of hardware support available from
tdx_caps, for example through the CPUID configs (0xAfor this one,0xDfor LBR and AMX)?It's wrong code. PMU is allowed.
Quoted 7 lines
Quoted 3 lines
AMX and LBR are disallowed because and the time we wrote the codes they are not supported by KVM. Now AMX should be allowed, but (arch-)LBR should be still blocked until KVM merges arch-LBR support.
I think Isaku's idea is we don't support them in the first submission?
If so as I suggested, we should add a TODO in comment..
Sure, will add a TODO comment.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [RFC PATCH v5 101/104] KVM: TDX: Silently ignore INIT/SIPI
In reply to Paolo BonziniQuoted 23 lines · Paolo Bonzini
Quoted 22 lines · Xiaoyao Li
Quoted 20 lines · Paolo Bonzini
Quoted 8 lines · On 3/4/22 20:49, [email protected] wrote:
+ if (
kvm_init_sipi_unsupported(vcpu->kvm))
+ /*
+ * TDX doesn't support INIT. Ignore INIT event. In the + * case of SIPI, the callback of
+ *vcpu_deliver_sipi_vectorignores it.
+ */
vcpu->arch.mp_state=KVM_MP_STATE_RUNNABLE; - elseQuoted 8 lines
-
vcpu->arch.mp_state=KVM_MP_STATE_INIT_RECEIVED; + else {
+kvm_vcpu_reset(vcpu, true);
+ if (kvm_vcpu_is_bsp(apic->vcpu))
+vcpu->arch.mp_state=KVM_MP_STATE_RUNNABLE; + else
+vcpu->arch.mp_state=KVM_MP_STATE_INIT_RECEIVED; + }Should you check
vcpu->arch.guest_state_protectedinstead of special-casing TDX?
Quoted 6 lines
Quoted 4 lines
We cannot use
vcpu->arch.guest_state_protectedbecause TDX supports debug TD, of which the states are not protected.At least we need another flag, I think.
Let's add .deliver_init to the
kvm_x86_opsthen.
Will do.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [RFC PATCH v5 102/104] KVM: TDX: Add methods to ignore accesses to CPU state
In reply to Paolo BonziniQuoted 12 lines · Paolo Bonzini
Quoted 7 lines · On 3/4/22 20:49, [email protected] wrote:
From: Sean Christopherson <[email protected]>
TDX protects TDX guest state from VMM. Implements to access methods for TDX guest state to ignore them or return zero.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>For most of these, it would be interesting to see which paths actually can be hit. For SEV, it's all cut out by
Quoted 7 lines
if (vcpu->arch.guest_state_protected) return 0;in functions such as
__set_sregs_common. Together with the fact that TDX does not get to e.g.handle_set_cr0, this should prevent most such calls from happening. So most of these should beKVM_BUG_ONorWARN_ON, not just returns.
If debug mode is enabled, guest state isn't protected. memory/cpu state can be read/written via SEAMCALLs. So guest_state_protected isn't set to true.
Anyway for now with this patch series, debug mode isn't supported well, I will go with adding KVM_BUG_ON/WARN_ON.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
In reply to Paolo BonziniQuoted 2 lines · Paolo Bonzini
You didn't answer the other question, which is "Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?" though... Should I be worried? :)
It's publicly documented.
Guest-Host-Communication Interface(GHCI) spec, 344426-003US Feburary 2022. 3.8 TDG.VP.VMCALL<Instruction.HLT>
R12 Interrupt Blocked Flag.
The TD is expected to clear this flag iff RFLAGS.IF == 1 or the TDCALL instruction (that invoked TDG.VP.TDVMCALL(Instruction.HLT)) immediately follows an STI instruction, otherwise this flag should be set.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [RFC PATCH v5 083/104] KVM: x86: Split core of hypercall emulation to helper function
In reply to Paolo BonziniQuoted 6 lines · Paolo Bonzini
Quoted 4 lines
arch/x86/kvm/x86.c int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) · line 9193
Quoted 4 lines
arch/x86/kvm/x86.c +4
-0+if (static_call (kvm_x86_get_cpl)(vcpu) !=0 ) { + ret = -KVM_EPERM ; + goto out; + }Is this guaranteed by TDG.VP.VMCALL?
Yes. TDCALL instruction in TD results in #GP(0) if CPL > 0. It's documented in trust domain CPU architectural extensions spec.
https://www.intel.com/content/dam/develop/external/us/en/documents-tps/intel-tdx-cpu-architectural-specification.pdf
Anyway VMM can't know TD guest CPL (or other CPU state).
Signature · 1 line
Isaku Yamahata <[email protected]>
Paolo Bonzini
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
In reply to Isaku YamahataQuoted 6 lines · Isaku Yamahata
Quoted 2 lines · Paolo Bonzini
You didn't answer the other question, which is "Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?" though... Should I be worried? :)
Quoted 8 lines
It's publicly documented.
Guest-Host-Communication Interface(GHCI) spec, 344426-003US Feburary 2022. 3.8 TDG.VP.VMCALL<Instruction.HLT>
R12 Interrupt Blocked Flag.
The TD is expected to clear this flag iff RFLAGS.IF == 1 or the TDCALL instruction (that invokedTDG.VP.TDVMCALL(Instruction.HLT)) immediately follows an STI instruction, otherwise this flag should be set.
Oh, Google doesn't know about this version of the spec... It can be downloaded from https://www.intel.com/content/www/us/en/developer/articles/technical/intel-trust-domain-extensions.html though.
I also found VCPU_STATE_DETAILS in https://www.intel.com/content/dam/develop/external/us/en/documents/tdx-module-1.0-public-spec-v0.931.pdf:
Bit 0: VMXIP, indicates that a virtual interrupt is pending delivery, i.e. VMCS.RVI[7:4] > TDVPS.VAPIC.VPPR[7:4]
It also documents how it has to be used. So this looks more or less okay, just rename "vmxip" to "interrupt_pending_delivery".
The VCPU_STATE_DETAILS being "non-architectural" is still worrisome.
Paolo
Sean Christopherson
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
Quoted 20 lines · Paolo Bonzini
Quoted 6 lines · Isaku Yamahata
Quoted 2 lines · Paolo Bonzini
You didn't answer the other question, which is "Where is R12 documented for TDG.VP.VMCALL<Instruction.HLT>?" though... Should I be worried? :)
Quoted 8 lines
It's publicly documented.
Guest-Host-Communication Interface(GHCI) spec, 344426-003US Feburary 2022. 3.8 TDG.VP.VMCALL<Instruction.HLT>
R12 Interrupt Blocked Flag.
The TD is expected to clear this flag iff RFLAGS.IF == 1 or the TDCALL instruction (that invokedTDG.VP.TDVMCALL(Instruction.HLT)) immediately follows an STI instruction, otherwise this flag should be set.Oh, Google doesn't know about this version of the spec... It can be
downloaded from https://www.intel.com/content/www/us/en/developer/articles/technical/intel-trust-domain-extensions.html
though.
Quoted 7 lines
I also found
VCPU_STATE_DETAILSin https://www.intel.com/content/dam/develop/external/us/en/documents/tdx-module-1.0-public-spec-v0.931.pdf:Bit 0: VMXIP, indicates that a virtual interrupt is pending delivery, i.e. VMCS.RVI[7:4] > TDVPS.VAPIC.VPPR[7:4]
It also documents how it has to be used. So this looks more or less okay, just rename "vmxip" to "
interrupt_pending_delivery".
If we're keeping the call back into SEAM, then this belongs in the path of apic_has_interrupt_for_ppr(), not in the HLT-exit path. To avoid multiple SEAMCALLS in a single exit, VCPU_EXREG_RVI can be added.
Sean Christopherson
maintainerRe: [RFC PATCH v5 075/104] KVM: x86: Check for pending APICv interrupt in kvm_vcpu_has_events()
In reply to [email protected]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 --
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 --
Sean Christopherson
maintainerRe: [RFC PATCH v5 003/104] KVM: TDX: Detect CPU feature on kernel module initialization
In reply to [email protected]Quoted 9 lines · On Fri, Mar 04, 2022, [email protected] wrote:
arch/x86/kvm/vmx/tdx.c new file +3
-0@@ -0,0 +1,53 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/cpu.h> +
arch/x86/kvm/vmx/tdx.c line 13
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c new file +10
-0+#include <asm/tdx.h> + +#include "capabilities.h" +#include "x86_ops.h" + +#undef pr_fmt +#define pr_fmt (fmt) "tdx: " fmt + +static bool __read_mostly enable_tdx = true; +module_param_named (tdx, enable_tdx, bool,0644 );
This is comically unsafe, userspace must not be allowed to toggle enable_tdx after KVM is loaded.
Quoted 28 lines
arch/x86/kvm/vmx/tdx.c new file +28
-0+static u64 hkid_mask__ro_after_init ; +static u8 hkid_start_pos__ro_after_init ; + +static int __init __tdx_hardware_setup (struct kvm_x86_ops *x86_ops) +{ +u32 max_pa; + +if (!enable_ept) { +pr_warn ("Cannot enable TDX with EPT disabled\n" ); +return -EINVAL ; + } + +if (!platform_has_tdx ()) { +pr_warn ("Cannot enable TDX with SEAMRR disabled\n" ); +return -ENODEV ; + } + +if (WARN_ON_ONCE (x86_ops->tlb_remote_flush)) +return -EIO ; + + max_pa =cpuid_eax (0x80000008 ) &0xff ; + hkid_start_pos = boot_cpu_data.x86_phys_bits; + hkid_mask =GENMASK_ULL (max_pa -1 , hkid_start_pos); + +return 0 ; +} + +void __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops)
arch/x86/kvm/vmx/tdx.c line 51
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c new file +10
-0+{ +/* +* This function is called at the initialization. No need to protect +* enable_tdx. +*/ +if (!enable_tdx) +return ; + +if (__tdx_hardware_setup (&vt_x86_ops)) + enable_tdx = false;
Clearing enable_tdx here unnecessarily risks introducing bugs in the caller, e.g. acting on enable_tdx before tdx_hardware_setup() is invoked. I'm guessing this was the result of trying to defer module load until VM creation. With that gone, the flag can be moved to vmx/main.c, as there should be zero reason for tdx.c to check/modify enable_tdx, i.e. functions in tdx.c should never be called if enabled_tdx = false.
An alteranative to
if (enable_tdx && tdx_hardware_setup(&vt_x86_ops)) enable_tdx = false;
would be
enable_tdx = enable_tdx && !tdx_hardware_setup(&vt_x86_ops);
I actually prefer the latter (no "if"), but I already generated and wiped the below diff before thinking of that.
arch/x86/kvm/vmx/main.c +5 -1
@@ -6,6 +6,9 @@#include "nested.h" #include "pmu.h" +static bool __read_mostly enable_tdx =IS_ENABLED (CONFIG_INTEL_TDX_HOST ); +module_param_named (tdx, enable_tdx, bool,0644 ); +static __init int vt_hardware_setup (void ) {int ret;
@@ -14,7 +17,8 @@ static __init int vt_hardware_setup(void)if (ret)return ret; -tdx_hardware_setup (&vt_x86_ops); +if (enable_tdx &&tdx_hardware_setup (&vt_x86_ops)) + enable_tdx = false;return 0 ; }
arch/x86/kvm/vmx/tdx.c +1 -17
@@ -9,13 +9,10 @@#undef pr_fmt#define pr_fmt (fmt) "tdx: " fmt -static bool __read_mostly enable_tdx = true; -module_param_named (tdx, enable_tdx, bool,0644 ); -static u64 hkid_mask__ro_after_init ;static u8 hkid_start_pos__ro_after_init ; -static int __init __tdx_hardware_setup (struct kvm_x86_ops *x86_ops) +static int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {u32 max_pa;
@@ -38,16 +35,3 @@ static int __init __tdx_hardware_setup(struct kvm_x86_ops *x86_ops)return 0 ; } - -void __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops) -{ -/* -* This function is called at the initialization. No need to protect -* enable_tdx. -*/ -if (!enable_tdx) -return ; - -if (__tdx_hardware_setup (&vt_x86_ops)) - enable_tdx = false; -}
arch/x86/kvm/vmx/x86_ops.h +2 -2
@@ -124,9 +124,9 @@ void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu);void vmx_setup_mce (struct kvm_vcpu *vcpu);#ifdef CONFIG_INTEL_TDX_HOST -void __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops); +int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);#else -static inline void tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {} +static inline int void tdx_hardware_setup(struct kvm_x86_ops *x86_ops) { return 0; }#endif #endif /* __KVM_X86_VMX_X86_OPS_H */
Re: [RFC PATCH v5 037/104] KVM: x86/mmu: Allow non-zero init value for shadow PTE
In reply to Paolo BonziniQuoted 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]>
Re: [RFC PATCH v5 042/104] KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
In reply to Paolo BonziniQuoted 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]>
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
Paolo Bonzini
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
In reply to Sean ChristophersonQuoted 6 lines · Sean Christopherson
Quoted 2 lines
It also documents how it has to be used. So this looks more or less okay, just rename "vmxip" to "
interrupt_pending_delivery".If we're keeping the call back into SEAM, then this belongs in the path of
apic_has_interrupt_for_ppr(), not in the HLT-exit path. To avoid multiple SEAMCALLS in a single exit,VCPU_EXREG_RVIcan be added.
But apic_has_interrupt_for_ppr takes a PPR argument and that is not available.
So I suppose you mean kvm_apic_has_interrupt? You would change that to a callback, like
if (!kvm_apic_present(vcpu))
return -1;
return static_call(kvm_x86_apic_has_interrupt)(vcpu);
}and the default version would also be inlined in kvm_get_apic_interrupt, like
- int vector = kvm_apic_has_interrupt(vcpu);
struct kvm_lapic *apic = vcpu->arch.apic;
u32 ppr;- if (vector == -1)
+ if (!kvm_apic_present(vcpu))
return -1;
+ __apic_update_ppr(apic, &ppr);
+ vector = apic_has_interrupt_for_ppr(apic, ppr);
Checking the SEAM state (which would likewise not be VCPU_EXREG_RVI, but more like VCPU_EXREG_INTR_PENDING) would be done in the tdx case of kvm_x86_apic_has_interrupt.
Paolo
Re: [RFC PATCH v5 102/104] KVM: TDX: Add methods to ignore accesses to CPU state
In reply to [email protected]Quoted 5 lines · On 3/5/2022 3:49 AM, [email protected] wrote:
From: Sean Christopherson <[email protected]>
TDX protects TDX guest state from VMM. Implements to access methods for TDX guest state to ignore them or return zero.
...
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +6
-0+void tdx_cache_reg (struct kvm_vcpu *vcpu,enum kvm_reg reg) +{ +kvm_register_mark_available (vcpu, reg); +switch (reg) { +case VCPU_REGS_RSP : +case VCPU_REGS_RIP :
arch/x86/kvm/vmx/tdx.c void tdx_enable_smi_window(struct kvm_vcpu *vcpu) · line 1747
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+case VCPU_EXREG_PDPTR : +case VCPU_EXREG_CR0 : +case VCPU_EXREG_CR3 : +case VCPU_EXREG_CR4 : +break ; +default : +KVM_BUG_ON (1 , vcpu->kvm); +break ; + } +}
Isaku,
We missed one case that some GPRs are accessible by KVM/userspace for TDVMCALL exit.
Paolo Bonzini
maintainerRe: [RFC PATCH v5 102/104] KVM: TDX: Add methods to ignore accesses to CPU state
Quoted 20 lines · Xiaoyao Li
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +6
-0+void tdx_cache_reg (struct kvm_vcpu *vcpu,enum kvm_reg reg) +{ + kvm_register_mark_available(vcpu, reg); + switch (reg) { + case VCPU_REGS_RSP: + case VCPU_REGS_RIP:arch/x86/kvm/vmx/tdx.c void tdx_enable_smi_window(struct kvm_vcpu *vcpu) · line 1747
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ case VCPU_EXREG_PDPTR: + case VCPU_EXREG_CR0: + case VCPU_EXREG_CR3: + case VCPU_EXREG_CR4: + break; + default: + KVM_BUG_ON(1, vcpu->kvm); + break; + } +}Isaku,
Quoted 1 line
We missed one case that some GPRs are accessible by KVM/userspace for TDVMCALL exit.
If a register is not in the VMX_REGS_LAZY_LOAD_SET it will never be passed to tdx_cache_reg. As far as I understand those TDVMCALL registers do not include either RSP or RIP.
Paolo
Re: [RFC PATCH v5 102/104] KVM: TDX: Add methods to ignore accesses to CPU state
Quoted 21 lines · Paolo Bonzini
Quoted 20 lines · Xiaoyao Li
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +6
-0+void tdx_cache_reg (struct kvm_vcpu *vcpu,enum kvm_reg reg) +{ + kvm_register_mark_available(vcpu, reg); + switch (reg) { + case VCPU_REGS_RSP: + case VCPU_REGS_RIP:arch/x86/kvm/vmx/tdx.c void tdx_enable_smi_window(struct kvm_vcpu *vcpu) · line 1747
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ case VCPU_EXREG_PDPTR: + case VCPU_EXREG_CR0: + case VCPU_EXREG_CR3: + case VCPU_EXREG_CR4: + break; + default: + KVM_BUG_ON(1, vcpu->kvm); + break; + } +}Isaku,
Quoted 3 lines
Quoted 1 line
We missed one case that some GPRs are accessible by KVM/userspace for TDVMCALL exit.
If a register is not in the
VMX_REGS_LAZY_LOAD_SETit will never be passed to tdx_cache_reg. As far as I understand those TDVMCALL registers do not include either RSP or RIP.
Sorry, I should not keep the code snippet of tdx_cache_reg() as reference to mislead you and other people.
I just want to remind that in the certain case of TDVMCALL, GPRs might be accessible.
Quoted 2 lines
Paolo
Sean Christopherson
maintainerRe: [RFC PATCH v5 092/104] KVM: TDX: Handle TDX PV HLT hypercall
In reply to Paolo BonziniQuoted 4 lines · Paolo Bonzini
Quoted 3 lines · Sean Christopherson
Quoted 2 lines
It also documents how it has to be used. So this looks more or less okay, just rename "vmxip" to "
interrupt_pending_delivery".
Quoted 8 lines
Quoted 3 lines
If we're keeping the call back into SEAM, then this belongs in the path of
apic_has_interrupt_for_ppr(), not in the HLT-exit path. To avoid multiple SEAMCALLS in a single exit,VCPU_EXREG_RVIcan be added.But
apic_has_interrupt_for_pprtakes a PPR argument and that is not available.So I suppose you mean
kvm_apic_has_interrupt?
Yeah, I realized that when I actually tried to implement my idea in code :-)
My hopefully-fully-thought-out idea for handling this:
https://lore.kernel.org/all/[email protected]
https://lore.kernel.org/all/[email protected]
Paolo Bonzini
maintainerRe: [RFC PATCH v5 024/104] KVM: TDX: create/destroy VM structure
In reply to Isaku YamahataQuoted 4 lines · Isaku Yamahata
Quoted 2 lines
Please rename the function to explain what it does, for example
tdx_mmu_release_hkid.In the patch 021/104, you suggested
flush_shadow_all_private(). Which do you prefer?flush_shadow_all_privateortdx_mmu_release_hkid.
vt_mmu_prezap should become vt_flush_shadow_all_private.
tdx_mmu_prezap should be come tdx_mmu_release_hkid.
Paolo
Re: [RFC PATCH v5 061/104] KVM: TDX: Finalize VM initialization
In reply to [email protected]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
Re: [RFC PATCH v5 065/104] KVM: TDX: vcpu_run: save/restore host state(host kernel gs)
In reply to [email protected]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 ; }
Re: [RFC PATCH v5 067/104] KVM: x86: Allow to update cached values in kvm_user_return_msrs w/o wrmsr
In reply to [email protected]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 ();
Re: [RFC PATCH v5 068/104] KVM: TDX: restore user ret MSRs
In reply to [email protected]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.
Re: [RFC PATCH v5 070/104] KVM: TDX: complete interrupts after tdexit
In reply to [email protected]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 ;
Re: [RFC PATCH v5 072/104] KVM: TDX: handle vcpu migration over logical processor
In reply to [email protected]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
Paolo Bonzini
maintainerRe: [RFC PATCH v5 075/104] KVM: x86: Check for pending APICv interrupt in kvm_vcpu_has_events()
In reply to Sean ChristophersonQuoted 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
Re: [RFC PATCH v5 084/104] KVM: TDX: Add a place holder to handle TDX VM exit
In reply to [email protected]Quoted 6 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up
handle_exitandhandle_exit_irqoffmethods and add a place holder to handle VM exit. Add helper functions to get exit info, exit qualification, etc.
Quoted 6 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 35 +++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 79 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 11 ++++++ 3 files changed, 122 insertions(+), 3 deletions(-)
Quoted 217 lines
arch/x86/kvm/vmx/main.c +32
-3@@ -148,6 +148,23 @@ static void vt_vcpu_load(struct kvm_vcpu *vcpu, int cpu)return vmx_vcpu_load (vcpu, cpu); } +static int vt_handle_exit (struct kvm_vcpu *vcpu, +enum exit_fastpath_completion fastpath) +{ +if (is_td_vcpu (vcpu)) +return tdx_handle_exit (vcpu, fastpath); + +return vmx_handle_exit (vcpu, fastpath); +} + +static void vt_handle_exit_irqoff (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_handle_exit_irqoff (vcpu); + +vmx_handle_exit_irqoff (vcpu); +} +static void vt_apicv_post_state_restore (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))@@ -340,6 +357,18 @@ static void vt_request_immediate_exit(struct kvm_vcpu *vcpu)vmx_request_immediate_exit (vcpu); } +static void vt_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason, +u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code) +{ +if (is_td_vcpu (vcpu)) { +tdx_get_exit_info (vcpu, reason, info1, info2, intr_info, + error_code); +return ; + } + +vmx_get_exit_info (vcpu, reason, info1, info2, intr_info, error_code); +} +static int vt_mem_enc_op (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))@@ -411,7 +440,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_pre_run = vmx_vcpu_pre_run, .run = vt_vcpu_run, - .handle_exit = vmx_handle_exit, + .handle_exit = vt_handle_exit, .skip_emulated_instruction = vmx_skip_emulated_instruction, .update_emulated_instruction = vmx_update_emulated_instruction, .set_interrupt_shadow = vt_set_interrupt_shadow,@@ -446,7 +475,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .set_identity_map_addr = vmx_set_identity_map_addr, .get_mt_mask = vmx_get_mt_mask, - .get_exit_info = vmx_get_exit_info, + .get_exit_info = vt_get_exit_info, .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid,@@ -460,7 +489,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .load_mmu_pgd = vt_load_mmu_pgd, .check_intercept = vmx_check_intercept, - .handle_exit_irqoff = vmx_handle_exit_irqoff, + .handle_exit_irqoff = vt_handle_exit_irqoff, .request_immediate_exit = vt_request_immediate_exit,arch/x86/kvm/vmx/tdx.c +79
-0@@ -68,6 +68,26 @@ static __always_inline hpa_t set_hkid_to_hpa(hpa_t pa, u16 hkid)return pa; } +static __always_inline unsigned long tdexit_exit_qual (struct kvm_vcpu *vcpu) +{ +return kvm_rcx_read (vcpu); +} + +static __always_inline unsigned long tdexit_ext_exit_qual (struct kvm_vcpu *vcpu) +{ +return kvm_rdx_read (vcpu); +} + +static __always_inline unsigned long tdexit_gpa (struct kvm_vcpu *vcpu) +{ +return kvm_r8_read (vcpu); +} + +static __always_inline unsigned long tdexit_intr_info (struct kvm_vcpu *vcpu) +{ +return kvm_r9_read (vcpu); +} +static inline bool is_td_vcpu_created (struct vcpu_tdx *tdx) {return tdx->tdvpr.added;@@ -768,6 +788,25 @@ void tdx_inject_nmi(struct kvm_vcpu *vcpu)td_management_write8 (to_tdx (vcpu),TD_VCPU_PEND_NMI ,1 ); } +void tdx_handle_exit_irqoff (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); +u16 exit_reason = tdx->exit_reason.basic; + +if (exit_reason ==EXIT_REASON_EXCEPTION_NMI ) +vmx_handle_exception_nmi_irqoff (vcpu,tdexit_intr_info (vcpu)); +else if (exit_reason ==EXIT_REASON_EXTERNAL_INTERRUPT ) +vmx_handle_external_interrupt_irqoff (vcpu, +tdexit_intr_info (vcpu)); +} + +static int tdx_handle_triple_fault (struct kvm_vcpu *vcpu) +{ + vcpu->run->exit_reason =KVM_EXIT_SHUTDOWN ; + vcpu->mmio_needed =0 ; +return 0 ; +} +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 );@@ -1042,6 +1081,46 @@ void tdx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,__vmx_deliver_posted_interrupt (vcpu, &tdx->pi_desc, vector); } +int tdx_handle_exit (struct kvm_vcpu *vcpu,fastpath_t fastpath) +{ +union tdx_exit_reason exit_reason =to_tdx (vcpu)->exit_reason; + +if (unlikely (exit_reason.non_recoverable || exit_reason.error)) { +if (exit_reason.basic ==EXIT_REASON_TRIPLE_FAULT ) +return tdx_handle_triple_fault (vcpu); + +kvm_pr_unimpl ("TD exit 0x%llx, %d\n" , + exit_reason.full, exit_reason.basic); + goto unhandled_exit; + } + +WARN_ON_ONCE (fastpath !=EXIT_FASTPATH_NONE ); + +switch (exit_reason.basic) { +default : +break ; + } + +unhandled_exit: + vcpu->run->exit_reason =KVM_EXIT_UNKNOWN ; + vcpu->run->hw.hardware_exit_reason = exit_reason.full; +return 0 ; +} + +void tdx_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason, +u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + + *reason = tdx->exit_reason.full; + + *info1 =tdexit_exit_qual (vcpu); + *info2 =tdexit_ext_exit_qual (vcpu); + + *intr_info =tdexit_intr_info (vcpu); + *error_code =0 ; +} +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 +11
-0@@ -146,11 +146,16 @@ 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); +void tdx_handle_exit_irqoff (struct kvm_vcpu *vcpu); +int tdx_handle_exit (struct kvm_vcpu *vcpu, +enum exit_fastpath_completion fastpath);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); +void tdx_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason, +u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);@@ -177,11 +182,17 @@ static inline fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) { return EXIT_FASTPstatic 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_handle_exit_irqoff (struct kvm_vcpu *vcpu) {} +static inline int tdx_handle_exit (struct kvm_vcpu *vcpu, +enum exit_fastpath_completion fastpath) {return 0 ; }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 void tdx_get_exit_info ( +struct kvm_vcpu *vcpu,u32 *reason,u64 *info1,u64 *info2, +u32 *intr_info,u32 *error_code) {}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 ; }
Re: [RFC PATCH v5 071/104] KVM: TDX: restore debug store when TD exit
In reply to [email protected]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;
Re: [RFC PATCH v5 085/104] KVM: TDX: handle EXIT_REASON_OTHER_SMI
In reply to [email protected]Quoted 29 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
If the control reaches
EXIT_REASON_OTHER_SMI, #SMI is delivered and handled right after returning from the TDX module to KVM nothing needs to be done in KVM. Continue TDX vcpu execution.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/uapi/asm/vmx.h | 1 + arch/x86/kvm/vmx/tdx.c | 7 +++++++ 2 files changed, 8 insertions(+)arch/x86/include/uapi/asm/vmx.h +1
-0@@ -34,6 +34,7 @@#define EXIT_REASON_TRIPLE_FAULT 2#define EXIT_REASON_INIT_SIGNAL 3#define EXIT_REASON_SIPI_SIGNAL 4 +#define EXIT_REASON_OTHER_SMI 6#define EXIT_REASON_INTERRUPT_WINDOW 7#define EXIT_REASON_NMI_WINDOW 8arch/x86/kvm/vmx/tdx.c +0
-0
arch/x86/kvm/vmx/tdx.c int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath) · line 1102
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +3
-0@@ -1097,6 +1097,13 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)WARN_ON_ONCE (fastpath !=EXIT_FASTPATH_NONE );switch (exit_reason.basic) { +case EXIT_REASON_OTHER_SMI : +/* +* If reach here, it's not a MSMI.
Please expand MSMI. Otherwise,
Paolo
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +4
-0+* #SMI is delivered and handled right after SEAMRET, nothing +* needs to be done in KVM. +*/ +return 1 ;default :break ; }
Re: [RFC PATCH v5 087/104] KVM: TDX: handle EXCEPTION_NMI and EXTERNAL_INTERRUPT
In reply to [email protected]Quoted 43 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Because guest TD state is protected, exceptions in guest TDs can't be intercepted. TDX VMM doesn't need to handle exceptions.
tdx_handle_exit_irqoff()handles NMI and machine check. Ignore NMI and machine check and continue guest TD execution.For external interrupt, increment stats same to the VMX case.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)arch/x86/kvm/vmx/tdx.c +17
-0@@ -800,6 +800,23 @@ void tdx_handle_exit_irqoff(struct kvm_vcpu *vcpu) tdexit_intr_info(vcpu)); } +static int tdx_handle_exception (struct kvm_vcpu *vcpu) +{ +u32 intr_info =tdexit_intr_info (vcpu); + +if (is_nmi (intr_info) ||is_machine_check (intr_info)) +return 1 ; + +kvm_pr_unimpl ("unexpected exception 0x%x\n" , intr_info); +return -EFAULT ; +} + +static int tdx_handle_external_interrupt (struct kvm_vcpu *vcpu) +{ + ++vcpu->stat.irq_exits; +return 1 ; +} +static int tdx_handle_triple_fault (struct kvm_vcpu *vcpu) { vcpu->run->exit_reason =KVM_EXIT_SHUTDOWN ;
arch/x86/kvm/vmx/tdx.c int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath) · line 1157
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +4
-0@@ -1131,6 +1148,10 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)WARN_ON_ONCE (fastpath !=EXIT_FASTPATH_NONE );switch (exit_reason.basic) { +case EXIT_REASON_EXCEPTION_NMI : +return tdx_handle_exception (vcpu); +case EXIT_REASON_EXTERNAL_INTERRUPT : +return tdx_handle_external_interrupt (vcpu);case EXIT_REASON_EPT_VIOLATION :return tdx_handle_ept_violation (vcpu);case EXIT_REASON_EPT_MISCONFIG :
Re: [RFC PATCH v5 088/104] KVM: TDX: Add TDG.VP.VMCALL accessors to access guest vcpu registers
In reply to [email protected]Quoted 10 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX defines ABI for the TDX guest to call hypercall with TDG.VP.VMCALL API. To get hypercall arguments and to set return values, add accessors to guest vcpu registers.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
Quoted 46 lines
arch/x86/kvm/vmx/tdx.c +35
-0@@ -88,6 +88,41 @@ static __always_inline unsigned long tdexit_intr_info(struct kvm_vcpu *vcpu)return kvm_r9_read (vcpu); } +#define BUILD_TDVMCALL_ACCESSORS (param, gpr) \ +static __always_inline \ +unsigned long tdvmcall_##param##_read(struct kvm_vcpu *vcpu) \ +{ \ + return kvm_##gpr##_read(vcpu); \ +} \ +static __always_inline void tdvmcall_##param##_write(struct kvm_vcpu *vcpu, \ + unsigned long val) \ +{ \ + kvm_##gpr##_write(vcpu, val); \ +} +BUILD_TDVMCALL_ACCESSORS (p1, r12); +BUILD_TDVMCALL_ACCESSORS (p2, r13); +BUILD_TDVMCALL_ACCESSORS (p3, r14); +BUILD_TDVMCALL_ACCESSORS (p4, r15); + +static __always_inline unsigned long tdvmcall_exit_type (struct kvm_vcpu *vcpu) +{ +return kvm_r10_read (vcpu); +} +static __always_inline unsigned long tdvmcall_exit_reason (struct kvm_vcpu *vcpu) +{ +return kvm_r11_read (vcpu); +} +static __always_inline void tdvmcall_set_return_code(struct kvm_vcpu *vcpu, +long val) +{ +kvm_r10_write (vcpu, val); +} +static __always_inline void tdvmcall_set_return_val(struct kvm_vcpu *vcpu, +unsigned long val) +{ +kvm_r11_write (vcpu, val); +} +static inline bool is_td_vcpu_created (struct vcpu_tdx *tdx) {return tdx->tdvpr.added;
Re: [RFC PATCH v5 093/104] KVM: TDX: Handle TDX PV port io hypercall
In reply to [email protected]Quoted 74 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV port IO hypercall to the KVM backend function.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)arch/x86/kvm/vmx/tdx.c +53
-0@@ -959,6 +959,59 @@ static int tdx_emulate_hlt(struct kvm_vcpu *vcpu)return kvm_emulate_halt_noskip (vcpu); } +static int tdx_complete_pio_in (struct kvm_vcpu *vcpu) +{ +struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; +unsigned long val =0 ; +int ret; + +WARN_ON (vcpu->arch.pio.count !=1 ); + + ret = ctxt->ops->pio_in_emulated (ctxt, vcpu->arch.pio.size, + vcpu->arch.pio.port, &val,1 ); +WARN_ON (!ret); + +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); +tdvmcall_set_return_val (vcpu, val); + +return 1 ; +} + +static int tdx_emulate_io (struct kvm_vcpu *vcpu) +{ +struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; +unsigned long val =0 ; +unsigned int port; +int size, ret; + + ++vcpu->stat.io_exits; + + size =tdvmcall_p1_read (vcpu); + port =tdvmcall_p3_read (vcpu); + +if (size !=1 && size !=2 && size !=4 ) { +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_INVALID_OPERAND ); +return 1 ; + } + +if (!tdvmcall_p2_read (vcpu)) { + ret = ctxt->ops->pio_in_emulated (ctxt, size, port, &val,1 ); +if (!ret) + vcpu->arch.complete_userspace_io = tdx_complete_pio_in; +else +tdvmcall_set_return_val (vcpu, val); + }else { + val =tdvmcall_p4_read (vcpu); + ret = ctxt->ops->pio_out_emulated (ctxt, size, port, &val,1 ); + +/* No need for a complete_userspace_io callback. */ + vcpu->arch.pio.count =0 ; + } +if (ret) +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); +return ret; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
arch/x86/kvm/vmx/tdx.c static int handle_tdvmcall(struct kvm_vcpu *vcpu) · line 1034
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +2
-0@@ -974,6 +1027,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)return tdx_emulate_cpuid (vcpu);case EXIT_REASON_HLT :return tdx_emulate_hlt (vcpu); +case EXIT_REASON_IO_INSTRUCTION : +return tdx_emulate_io (vcpu);default :break ; }
Re: [RFC PATCH v5 094/104] KVM: TDX: Handle TDX PV MMIO hypercall
In reply to [email protected]Quoted 176 lines · On 3/4/22 20:49, [email protected] wrote:
From: Sean Christopherson <[email protected]>
Export
kvm_io_bus_readandkvm_mmiotracepoint and wire up TDX PV MMIO hypercall to the KVM backend functions.kvm_io_bus_read/write() searches KVM device emulated in kernel of the given MMIO address and emulates the MMIO. As TDX PV MMIO also needs it, export
kvm_io_bus_read().kvm_io_bus_write()is already exported. TDX PV MMIO emulates some of MMIO itself. To add trace point consistently with x86 kvm, exportkvm_mmiotracepoint.Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 114 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 1 + virt/kvm/kvm_main.c | 2 + 3 files changed, 117 insertions(+)arch/x86/kvm/vmx/tdx.c +114
-0@@ -1012,6 +1012,118 @@ static int tdx_emulate_io(struct kvm_vcpu *vcpu)return ret; } +static int tdx_complete_mmio (struct kvm_vcpu *vcpu) +{ +unsigned long val =0 ; +gpa_t gpa; +int size; + +WARN_ON (vcpu->mmio_needed !=1 ); + vcpu->mmio_needed =0 ; + +if (!vcpu->mmio_is_write) { + gpa = vcpu->mmio_fragments[0 ].gpa; + size = vcpu->mmio_fragments[0 ].len; + +memcpy (&val, vcpu->run->mmio.data, size); +tdvmcall_set_return_val (vcpu, val); +trace_kvm_mmio (KVM_TRACE_MMIO_READ , size, gpa, &val); + } +return 1 ; +} + +static inline int tdx_mmio_write (struct kvm_vcpu *vcpu,gpa_t gpa,int size, +unsigned long val) +{ +if (kvm_iodevice_write (vcpu, &vcpu->arch.apic->dev, gpa, size, &val) && +kvm_io_bus_write (vcpu,KVM_MMIO_BUS , gpa, size, &val)) +return -EOPNOTSUPP ; + +trace_kvm_mmio (KVM_TRACE_MMIO_WRITE , size, gpa, &val); +return 0 ; +} + +static inline int tdx_mmio_read (struct kvm_vcpu *vcpu,gpa_t gpa,int size) +{ +unsigned long val; + +if (kvm_iodevice_read (vcpu, &vcpu->arch.apic->dev, gpa, size, &val) && +kvm_io_bus_read (vcpu,KVM_MMIO_BUS , gpa, size, &val)) +return -EOPNOTSUPP ; + +tdvmcall_set_return_val (vcpu, val); +trace_kvm_mmio (KVM_TRACE_MMIO_READ , size, gpa, &val); +return 0 ; +} + +static int tdx_emulate_mmio (struct kvm_vcpu *vcpu) +{ +struct kvm_memory_slot *slot; +int size, write, r; +unsigned long val; +gpa_t gpa; + +WARN_ON (vcpu->mmio_needed); + + size =tdvmcall_p1_read (vcpu); + write =tdvmcall_p2_read (vcpu); + gpa =tdvmcall_p3_read (vcpu); + val = write ?tdvmcall_p4_read (vcpu) :0 ; + +if (size !=1 && size !=2 && size !=4 && size !=8 ) + goto error; +if (write !=0 && write !=1 ) + goto error; + +/* Strip the shared bit, allow MMIO with and without it set. */ + gpa =kvm_gpa_unalias (vcpu->kvm, gpa); + +if (size >8u || ((gpa + size -1 ) ^ gpa) &PAGE_MASK ) + goto error; + + slot =kvm_vcpu_gfn_to_memslot (vcpu,gpa_to_gfn (gpa)); +if (slot && !(slot->flags &KVM_MEMSLOT_INVALID )) + goto error; + +if (!kvm_io_bus_write (vcpu,KVM_FAST_MMIO_BUS , gpa,0 ,NULL )) { +trace_kvm_fast_mmio (gpa); +return 1 ; + } + +if (write) + r =tdx_mmio_write (vcpu, gpa, size, val); +else + r =tdx_mmio_read (vcpu, gpa, size); +if (!r) { +/* Kernel completed device emulation. */ +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); +return 1 ; + } + +/* Request the device emulation to userspace device model. */ + vcpu->mmio_needed =1 ; + vcpu->mmio_is_write = write; + vcpu->arch.complete_userspace_io = tdx_complete_mmio; + + vcpu->run->mmio.phys_addr = gpa; + vcpu->run->mmio.len = size; + vcpu->run->mmio.is_write = write; + vcpu->run->exit_reason =KVM_EXIT_MMIO ; + +if (write) { +memcpy (vcpu->run->mmio.data, &val, size); + }else { + vcpu->mmio_fragments[0 ].gpa = gpa; + vcpu->mmio_fragments[0 ].len = size; +trace_kvm_mmio (KVM_TRACE_MMIO_READ_UNSATISFIED , size, gpa,NULL ); + } +return 0 ; + +error: +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); +return 1 ; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);@@ -1029,6 +1141,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)return tdx_emulate_hlt (vcpu);case EXIT_REASON_IO_INSTRUCTION :return tdx_emulate_io (vcpu); +case EXIT_REASON_EPT_VIOLATION : +return tdx_emulate_mmio (vcpu);default :break ; }arch/x86/kvm/x86.c +1
-0@@ -12915,6 +12915,7 @@ bool kvm_arch_dirty_log_supported(struct kvm *kvm)EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_entry);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_exit); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_mmio);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_fast_mmio);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_inj_virq);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_page_fault);virt/kvm/kvm_main.c +1
-0@@ -2259,6 +2259,7 @@ struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfnreturn NULL ; } +EXPORT_SYMBOL_GPL (kvm_vcpu_gfn_to_memslot);bool kvm_is_visible_gfn (struct kvm *kvm,gfn_t gfn) {
virt/kvm/kvm_main.c int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, · line 5133
Quoted 7 lines
virt/kvm/kvm_main.c +1
-0@@ -5126,6 +5127,7 @@ int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, r =__kvm_io_bus_read (vcpu, bus, &range, val);return r <0 ? r :0 ; } +EXPORT_SYMBOL_GPL (kvm_io_bus_read);/* Caller must hold slots_lock. */ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
Re: [RFC PATCH v5 095/104] KVM: TDX: Implement callbacks for MSR operations for TDX
In reply to [email protected]Quoted 172 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Implements set_msr/get_msr/has_emulated_msr methods for TDX to handle hypercall from guest TD for paravirtualized rdmsr and wrmsr. The TDX module virtualizes MSRs. For some MSRs, it injects #VE to the guest TD upon RDMSR or WRMSR. The exact list of such MSRs are defined in the spec.
Upon #VE, the guest TD may execute hypercalls, TDG.VP.VMCALL<INSTRUCTION.RDMSR> and TDG.VP.VMCALL<INSTRUCTION.WRMSR>, which are defined in GHCI (Guest-Host Communication Interface) so that the host VMM (e.g. KVM) can virtualizes the MSRs.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 34 +++++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 68 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 6 ++++ 3 files changed, 105 insertions(+), 3 deletions(-)arch/x86/kvm/vmx/main.c +31
-3@@ -165,6 +165,34 @@ static void vt_handle_exit_irqoff(struct kvm_vcpu *vcpu)vmx_handle_exit_irqoff (vcpu); } +static int vt_set_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) +{ +if (unlikely (is_td_vcpu (vcpu))) +return tdx_set_msr (vcpu, msr_info); + +return vmx_set_msr (vcpu, msr_info); +} + +/* +* The kvm parameter can be NULL (module initialization, or invocation before +* VM creation). Be sure to check the kvm parameter before using it. +*/ +static bool vt_has_emulated_msr (struct kvm *kvm,u32 index) +{ +if (kvm &&is_td (kvm)) +return tdx_is_emulated_msr (index, true); + +return vmx_has_emulated_msr (kvm, index); +} + +static int vt_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) +{ +if (unlikely (is_td_vcpu (vcpu))) +return tdx_get_msr (vcpu, msr_info); + +return vmx_get_msr (vcpu, msr_info); +} +static void vt_apicv_post_state_restore (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu))@@ -393,7 +421,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .hardware_enable = vt_hardware_enable, .hardware_disable = vt_hardware_disable, .cpu_has_accelerated_tpr = report_flexpriority, - .has_emulated_msr = vmx_has_emulated_msr, + .has_emulated_msr = vt_has_emulated_msr, .is_vm_type_supported = vt_is_vm_type_supported, .vm_size = sizeof(struct kvm_vmx),@@ -411,8 +439,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .update_exception_bitmap = vmx_update_exception_bitmap, .get_msr_feature = vmx_get_msr_feature, - .get_msr = vmx_get_msr, - .set_msr = vmx_set_msr, + .get_msr = vt_get_msr, + .set_msr = vt_set_msr, .get_segment_base = vmx_get_segment_base, .get_segment = vmx_get_segment, .set_segment = vmx_set_segment,arch/x86/kvm/vmx/tdx.c +68
-0@@ -1517,6 +1517,74 @@ void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, *error_code =0 ; } +bool tdx_is_emulated_msr (u32 index,bool write) +{ +switch (index) { +case MSR_IA32_UCODE_REV : +case MSR_IA32_ARCH_CAPABILITIES : +case MSR_IA32_POWER_CTL : +case MSR_MTRRcap: + case 0x200 ... 0x26f: +/* IA32_MTRR_PHYS{BASE, MASK}, IA32_MTRR_FIX*_* */ +case MSR_IA32_CR_PAT : +case MSR_MTRRdefType: +case MSR_IA32_TSC_DEADLINE : +case MSR_IA32_MISC_ENABLE : +case MSR_KVM_STEAL_TIME : +case MSR_KVM_POLL_CONTROL : +case MSR_PLATFORM_INFO : +case MSR_MISC_FEATURES_ENABLES : +case MSR_IA32_MCG_CAP : +case MSR_IA32_MCG_STATUS : +case MSR_IA32_MCG_CTL : +case MSR_IA32_MCG_EXT_CTL : + case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_MISC(28) - 1: +/* MSR_IA32_MCx_{CTL, STATUS, ADDR, MISC} */ +return true; + case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff: +/* +* x2APIC registers that are virtualized by the CPU can't be +* emulated, KVM doesn't have access to the virtual APIC page. +*/ +switch (index) { +case X2APIC_MSR (APIC_TASKPRI ): +case X2APIC_MSR (APIC_PROCPRI ): +case X2APIC_MSR (APIC_EOI ): + case X2APIC_MSR(APIC_ISR) ... X2APIC_MSR(APIC_ISR + APIC_ISR_NR): + case X2APIC_MSR(APIC_TMR) ... X2APIC_MSR(APIC_TMR + APIC_ISR_NR): + case X2APIC_MSR(APIC_IRR) ... X2APIC_MSR(APIC_IRR + APIC_ISR_NR): +return false; +default : +return true; + } +case MSR_IA32_APICBASE : +case MSR_EFER : +return !write; + case MSR_IA32_MCx_CTL2(0) ... MSR_IA32_MCx_CTL2(31): +/* +* 0x280 - 0x29f: The x86 common code doesn't emulate MCx_CTL2. +* Refer to kvm_{get,set}_msr_common(), +* kvm_mtrr_{get, set}_msr(), and msr_mtrr_valid(). +*/ +default : +return false; + } +} + +int tdx_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +if (tdx_is_emulated_msr (msr->index, false)) +return kvm_get_msr_common (vcpu, msr); +return 1 ; +} + +int tdx_set_msr (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +if (tdx_is_emulated_msr (msr->index, true)) +return kvm_set_msr_common (vcpu, msr); +return 1 ; +} +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 +3
-0@@ -156,6 +156,9 @@ void tdx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,void tdx_inject_nmi (struct kvm_vcpu *vcpu);void tdx_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason,u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code); +bool tdx_is_emulated_msr (u32 index,bool write); +int tdx_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr); +int tdx_set_msr (struct kvm_vcpu *vcpu,struct msr_data *msr);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_inject_nmi(struct kvm_vcpu *vcpu) {} · line 204
Quoted 9 lines
arch/x86/kvm/vmx/x86_ops.h +3
-0@@ -193,6 +196,9 @@ static inline void tdx_inject_nmi(struct kvm_vcpu *vcpu) {}static inline void tdx_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason,u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code) {} +static inline bool tdx_is_emulated_msr (u32 index,bool write) {return false; } +static inline int tdx_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr) {return 1 ; } +static inline int tdx_set_msr (struct kvm_vcpu *vcpu,struct msr_data *msr) {return 1 ; }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 ; }
Re: [RFC PATCH v5 096/104] KVM: TDX: Handle TDX PV rdmsr hypercall
In reply to [email protected]Quoted 38 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV rdmsr hypercall to the KVM backend function.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)arch/x86/kvm/vmx/tdx.c +17
-0@@ -1124,6 +1124,23 @@ static int tdx_emulate_mmio(struct kvm_vcpu *vcpu)return 1 ; } +static int tdx_emulate_rdmsr (struct kvm_vcpu *vcpu) +{ +u32 index =tdvmcall_p1_read (vcpu); +u64 data; + +if (kvm_get_msr (vcpu, index, &data)) { +trace_kvm_msr_read_ex (index); +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_INVALID_OPERAND ); +return 1 ; + } +trace_kvm_msr_read (index, data); + +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS ); +tdvmcall_set_return_val (vcpu, data); +return 1 ; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);
arch/x86/kvm/vmx/tdx.c static int handle_tdvmcall(struct kvm_vcpu *vcpu) · line 1167
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +2
-0@@ -1143,6 +1160,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)return tdx_emulate_io (vcpu);case EXIT_REASON_EPT_VIOLATION :return tdx_emulate_mmio (vcpu); +case EXIT_REASON_MSR_READ : +return tdx_emulate_rdmsr (vcpu);default :break ; }
and feel free to squash with the wrmsr one.
Paolo
Paolo Bonzini
maintainerRe: [RFC PATCH v5 098/104] KVM: TDX: Handle TDX PV report fatal error hypercall
In reply to [email protected]Quoted 16 lines · On 3/4/22 20:49, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV report fatal error hypercall to
KVM_SYSTEM_EVENT_CRASHKVM exit event.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 11 +++++++++++ 1 file changed, 11 insertions(+)arch/x86/kvm/vmx/tdx.c +0
-0@@ -1157,6 +1157,15 @@ static int tdx_emulate_wrmsr(struct kvm_vcpu *vcpu)return 1 ;
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_wrmsr(struct kvm_vcpu *vcpu) · line 1167
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +8
-0} +static int tdx_report_fatal_error (struct kvm_vcpu *vcpu) +{ +/* Exit to userspace device model for teardown. */ + vcpu->run->exit_reason =KVM_EXIT_SYSTEM_EVENT ; + vcpu->run->system_event.type =KVM_SYSTEM_EVENT_CRASH ; + vcpu->run->system_event.flags =tdvmcall_p1_read (vcpu); +return 0 ; +}
With the latest SEV changes we have a data[] member. Please set type instead to KVM_SYSTEM_EVENT_CRASH|KVM_SYSTEM_EVENT_NDATA_VALID and ndata to 1, so that the value of p1 (which will be a0 in the next series) can go in data[0].
Paolo
Paolo Bonzini
maintainerQuoted 11 lines · On 3/4/22 20:48, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Hi. Now TDX host kernel patch series was posted, I've rebased this patch series to it and make it work.
https://lore.kernel.org/lkml/[email protected]/Changes from v4:
- rebased to TDX host kernel patch series.
- include all the patches to make this patch series working.
- add [MARKER] patches to mark the patch layer clear.
I think I have reviewed everything except the TDP MMU parts (48, 54-57).
I will do those next week, but in the meanwhile feel free to send v6
if you have it ready. A lot of the requests have been cosmetic.
If you would like to use something like Trello to track all the changes, and submit before you have done all of them, that's fine by me.
Paolo
Quoted 620 lines
Thanks,
* What's TDX?
TDX stands for Trust Domain Extensions, which extends Intel Virtual Machines Extensions (VMX) to introduce a kind of virtual machine guest called a Trust Domain (TD) for confidential computing.A TD runs in a CPU mode that is designed to protect the confidentiality of its memory contents and its CPU state from any other software, including the hosting Virtual Machine Monitor (VMM), unless explicitly shared by the TD itself.
We have more detailed explanations below (***). We have the high-level design of TDX KVM below (****).
In this patch series, we use "TD" or "guest TD" to differentiate it from the current "VM" (Virtual Machine), which is supported by KVM today.
* The organization of this patch series
This patch series is on top of the patches series "TDX host kernel support":
https://lore.kernel.org/lkml/[email protected]/this patch series is available at
https://github.com/intel/tdx/releases/tag/kvm-upstream
The corresponding patches to qemu are available at
https://github.com/intel/qemu-tdx/commits/tdx-upstreamThe relations of the layers are depicted as follows.
The arrows below show the order of patch reviews we would like to have.The below layers are chosen so that the device model, for example, qemu can exercise each layering step by step. Check if TDX is supported, create TD VM, create TD vcpu, allow vcpu running, populate TD guest private memory, and handle vcpu exits/hypercalls/interrupts to run TD fully.
TDX vcpu
interrupt/exits/hypercall<------------\^ | | | TD finalization | ^ | | | TDX EPT violation<------------\ | ^ | | | | | TD vcpu enter/exit | | ^ | | | | | TD vcpu creation/destruction | \-------KVM TDP MMU MapGPA ^ | ^ | | | TD VM creation/destruction \---------------KVM TDP MMU hooks^ ^
| |
TDX architectural definitions KVM TDP refactoring for TDX
^ ^
| |
TDX, VMX <--------TDX host kernel KVM MMU GPA stolen bits
coexistence supportThe followings are explanations of each layer. Each layer has a dummy commit that starts with [MARKER] in subject. It is intended to help to identify where each layer starts.
TDX host kernel support:
https://lore.kernel.org/lkml/[email protected]/
The guts of system-wide initialization of TDX module. There is an independent patch series for host x86. TDX KVM patches call functions this patch series provides to initialize the TDX module.TDX, VMX coexistence:
Infrastructure to allow TDX to coexist with VMX and trigger the initialization of the TDX module.
This layer starts with
"KVM: VMX: Move outvmx_x86_opsto 'main.c' to wrap VMX and TDX"
TDX architectural definitions:
Add TDX architectural definitions and helper functions
This layer starts with
"[MARKER] The start of TDX KVM patch series: TDX architectural definitions".
TD VM creation/destruction:
Guest TD creation/destroy allocation and releasing of TDX specific vm and vcpu structure. Create an initial guest memory image with TDX measurement.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD VM creation/destruction".
TD vcpu creation/destruction:
guest TD creation/destroy Allocation and releasing of TDX specific vm and vcpu structure. Create an initial guest memory image with TDX measurement.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD vcpu creation/destruction"
TDX EPT violation:
Create an initial guest memory image with TDX measurement. Handle secure EPT violations to populate guest pages with TDX SEAMCALLs. This layer starts with
"[MARKER] The start of TDX KVM patch series: TDX EPT violation"
TD vcpu enter/exit:
Allow TDX vcpu to enter into TD and exit from TD. Save CPU state before entering into TD. Restore CPU state after exiting from TD.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD vcpu enter/exit"
TD vcpu interrupts/exit/hypercall:
Handle various exits/hypercalls and allow interrupts to be injected so that TD vcpu can continue running.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD vcpu exits/interrupts/hypercalls"KVM MMU GPA stolen bits:
Introduce framework to handle stolen repurposed bit of GPA TDX repurposed a bit of GPA to indicate shared or private. If it's shared, it's the same as the conventional VMX EPT case. VMM can access shared guest pages. If it's private, it's handled by Secure-EPT and the guest page is encrypted.
This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM MMU GPA stolen bits"
KVM TDP refactoring for TDX:
TDX Secure EPT requires different constants. e.g. initial value EPT entry value etc. Various refactoring for those differences. This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM TDP refactoring for TDX"
KVM TDP MMU hooks:
Introduce framework to TDP MMU to add hooks in addition to direct EPT access TDX added Secure EPT which is an enhancement to VMX EPT. Unlike conventional VMX EPT, CPU can't directly read/write Secure EPT. Instead, use TDX SEAMCALLs to operate on Secure EPT.
This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM TDP MMU hooks"
KVM TDP MMU MapGPA:
Introduce framework to handle switching guest pages from private/shared to shared/private. For a given GPA, a guest page can be assigned to a private GPA or a shared GPA exclusively. With TDX MapGPA hypercall, guest TD converts GPA assignments from private (or shared) to shared (or private).
This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM TDP MMU MapGPA "KVM guest private memory: (not shown in the above diagram)
[PATCH v4 00/12] KVM: mm: fd-based approach for supporting KVM guest private
memory: https://lkml.org/lkml/2022/1/18/395
Guest private memory requires different memory management in KVM. The patch proposes a way for it. Integration with TDX KVM.(***)
* TDX module
A CPU-attested software module called the "TDX module" is designed to implement the TDX architecture, and it is loaded by the UEFI firmware today. It can be loaded by the kernel or driver at runtime, but in this patch series we assume that the TDX module is already loaded and initialized.The TDX module provides two main new logical modes of operation built upon the new SEAM (Secure Arbitration Mode) root and non-root CPU modes added to the VMX architecture. TDX root mode is mostly identical to the VMX root operation mode, and the TDX functions (described later) are triggered by the new SEAMCALL instruction with the desired interface function selected by an input operand (leaf number, in RAX). TDX non-root mode is used for TD guest operation. TDX non-root operation (i.e. "guest TD" mode) is similar to the VMX non-root operation (i.e. guest VM), with changes and restrictions to better assure that no other software or hardware has direct visibility of the TD memory and state.
TDX transitions between TDX root operation and TDX non-root operation include TD Entries, from TDX root to TDX non-root mode, and TD Exits from TDX non-root to TDX root mode. A TD Exit might be asynchronous, triggered by some external event (e.g., external interrupt or SMI) or an exception, or it might be synchronous, triggered by a TDCALL (TDG.VP.VMCALL) function.
TD VCPUs can be entered using SEAMCALL(TDH.VP.ENTER) by KVM. TDH.VP.ENTER is one of the TDX interface functions as mentioned above, and "TDH" stands for Trust Domain Host. Those host-side TDX interface functions are categorized into various areas just for better organization, such as SYS (TDX module management), MNG (TD management), VP (VCPU), PHYSMEM (physical memory), MEM (private memory), etc. For example, SEAMCALL(TDH.SYS.INFO) returns the TDX module information.
TDCS (Trust Domain Control Structure) is the main control structure of a guest TD, and encrypted (using the guest TD's ephemeral private key). At a high level, TDCS holds information for controlling TD operation as a whole, execution, EPTP, MSR bitmaps, etc that KVM needs to set it up. Note that MSR bitmaps are held as part of TDCS (unlike VMX) because they are meant to have the same value for all VCPUs of the same TD.
Trust Domain Virtual Processor State (TDVPS) is the root control structure of a TD VCPU. It helps the TDX module control the operation of the VCPU, and holds the VCPU state while the VCPU is not running. TDVPS is opaque to software and DMA access, accessible only by using the TDX module interface functions (such as TDH.VP.RD, TDH.VP.WR). TDVPS includes TD VMCS, and TD VMCS auxiliary structures, such as virtual APIC page, virtualization exception information, etc.
Several VMX control structures (such as Shared EPT and Posted interrupt descriptor) are directly managed and accessed by the host VMM. These control structures are pointed to by fields in the TD VMCS.
The above means that 1) KVM needs to allocate different data structures for TDs,
2) KVM can reuse the existing code for TDs for some operations, 3) it needs to define TD-specific handling for others. 3) Redirect operations to . 3) Redirect operations to the TDX specific callbacks, like "if (is_td_vcpu(vcpu))tdx_callback()elsevmx_callback();".*TD Private Memory
TD private memory is designed to hold TD private content, encrypted by the CPU using the TD ephemeral key. An encryption engine holds a table of encryption keys, and an encryption key is selected for each memory transaction based on a Host Key Identifier (HKID). By design, the host VMM does not have access to the encryption keys.In the first generation of MKTME, HKID is "stolen" from the physical address by allocating a configurable number of bits from the top of the physical address. The HKID space is partitioned into shared HKIDs for legacy MKTME accesses and private HKIDs for SEAM-mode-only accesses. We use 0 for the shared HKID on the host so that MKTME can be opaque or bypassed on the host.
During TDX non-root operation (i.e. guest TD), memory accesses can be qualified as either shared or private, based on the value of a new SHARED bit in the Guest Physical Address (GPA). The CPU translates shared GPAs using the usual VMX EPT (Extended Page Table) or "Shared EPT" (in this document), which resides in host VMM memory. The Shared EPT is directly managed by the host VMM - the same as with the current VMX. Since guest TDs usually require I/O, and the data exchange needs to be done via shared memory, thus KVM needs to use the current EPT functionality even for TDs.
* Secure EPT and Minoring using the TDP code
The CPU translates private GPAs using a separate Secure EPT. The Secure EPT pages are encrypted and integrity-protected with the TD's ephemeral private key. Secure EPT can be managed _indirectly_ by the host VMM, using the TDX interface functions, and thus conceptually Secure EPT is a subset of EPT (why "subset"). Since execution of such interface functions takes much longer time than accessing memory directly, in KVM we use the existing TDP code to minor the Secure EPT for the TD.This way, we can effectively walk Secure EPT without using the TDX interface functions.
* VM life cycle and TDX specific operations
The userspace VMM, such as QEMU, needs to build and treat TDs differently. For example, a TD needs to boot in private memory, and the host software cannot copy the initial image to private memory.* TSC Virtualization
The TDX module helps TDs maintain reliable TSC (Time Stamp Counter) values (e.g. consistent among the TD VCPUs) and the virtual TSC frequency is determined by TD configuration, i.e. when the TD is created, not per VCPU. The current KVM owns TSC virtualization for VMs, but the TDX module does for TDs.* MCE support for TDs
The TDX module doesn't allow VMM to inject MCE. Instead PV way is needed for TD to communicate with VMM. For now, KVM silently ignores MCE request by VMM. MSRs related to MCE (e.g, MCE bank registers) can be naturally emulated by paravirtualizing MSR access.[1] For details, the specifications, [2], [3], [4], [5], [6], [7], are available.
* Restrictions or future work
Some features are not included to reduce patch size. Those features are addressed as future independent patch series.
- large page (2M, 1G)
- qemu gdb stub
- guest PMU
- and more* Prerequisites
It's required to load the TDX module and initialize it. It's out of the scope of this patch series. Another independent patch for the common x86 code is planned. It definesCONFIG_INTEL_TDX_HOSTand this patch series usesCONFIG_INTEL_TDX_HOST. It's assumed that WithCONFIG_INTEL_TDX_HOST=y, the TDX module is initialized and ready for KVM to use the TDX module APIs for TDX guest life cycle like tdh.mng.init are ready to use.Concretely Global initialization, LP (Logical Processor) initialization, global configuration, the key configuration, and TDMR and PAMT initialization are done. The state of the TDX module is
SYS_READY. Please refer to the TDX module specification, the chapter Intel TDX Module Lifecycle State Machine** Detecting the TDX module readiness.
TDX host patch series implements the detection of the TDX module availability and its initialization so that KVM can use it. Also it manages Host KeyID (HKID) assigned to guest TD.
The assumed APIs the TDX host patch series provides are
- intseamrr_enabled()
Check if required cpu feature (SEAM mode) is available. This only check CPU feature availability. At this point, the TDX module may not be ready for KVM to use.
- intinit_tdx(void);
Initialization of TDX module so that the TDX module is ready for KVM to use.
- const structtdsysinfo_struct*tdx_get_sysinfo(void);
Return the system wide information about the TDX module. NULL if the TDX isn't initialized.
- u32tdx_get_global_keyid(void);
Return global key id that is used for the TDX module itself.
- inttdx_keyid_alloc(void);
Allocate HKID for guest TD.
- voidtdx_keyid_free(int keyid);
Free HKID for guest TD.(****)
* TDX KVM high-level design
- Host key ID management
Host Key ID (HKID) needs to be assigned to each TDX guest for memory encryption. It is assumed The TDX host patch series implements necessary functions, u32tdx_get_global_keyid(void), inttdx_keyid_alloc(void)and,
voidtdx_keyid_free(int keyid).- Data structures and VM type
Because TDX is different from VMX, define its own VM/VCPU structures, structkvm_tdxand structvcpu_tdxinstead of structkvm_vmxand structvcpu_vmx. To identify the VM, introduce VM-type to specify which VM type, VMX (default) or TDX, is used.- VM life cycle and TDX specific operations
Re-purpose the existingKVM_MEMORY_ENCRYPT_OPto add TDX specific operations. New commands are used to get the TDX system parameters, set TDX specific VM/VCPU parameters, set initial guest memory and measurement.The creation of TDX VM requires five additional operations in addition to the conventional VM creation.
- Get KVM system capability to check if TDX VM type is supported
- VM creation (KVM_CREATE_VM)
- New: Get the TDX specific system parameters.KVM_TDX_GET_CAPABILITY.
- New: Set TDX specific VM parameters.KVM_TDX_INIT_VM.
- VCPU creation (KVM_CREATE_VCPU)
- New: Set TDX specific VCPU parameters.KVM_TDX_INIT_VCPU.
- New: Initialize guest memory as boot state and extend the measurement with the memory.KVM_TDX_INIT_MEM_REGION.
- New: Finalize VM.KVM_TDX_FINALIZE. Complete measurement of the initial TDX VM contents.
- VCPU RUN (KVM_VCPU_RUN)- Protected guest state
Because the guest state (CPU state and guest memory) is protected, the KVM VMM can't operate on them. For example, accessing CPU registers, injecting exceptions, and accessing guest memory. Those operations are handled as silently ignored, returning zero or initial reset value when it's requested via KVM API ioctls.VM/VCPU state and callbacks for TDX specific operations. Define tdx specific VM state and VCPU state instead of VMX ones. Redirect operations to TDX specific callbacks. "if (tdx)
tdx_op()elsevmx_op()".Operations on the CPU state
silently ignore operations on the guest state. For example, the write to CPU registers is ignored and the read from CPU registers returns 0.. ignore access to CPU registers except for allowed ones.
. TSC: add a check if tsc is immutable and return an error. Because the KVM
implementation updates the internal tsc state and it's difficult to back out those changes. Instead, skip the logic.
. dirty logging: add check if dirty logging is supported.
. exceptions/SMI/MCE/SIPI/INIT: silently ignoreNote: virtual external interrupt and NMI can be injected into TDX guests.
- KVM MMU integration
One bit of the guest physical address (bit 51 or 47) is repurposed to indicate if the guest physical address is private (the bit is cleared) or shared (the bit is set). The bits are called stolen bits.- Stolen bits framework
systematically tracks which guest physical address, shared or private, is used.- Shared EPT and secure EPT
There are two EPTs. Shared EPT (the conventional one) and Secure EPT(the new one). Shared EPT is handled the same for the stolen bit set. Secure EPT points to private guest pages. To resolve EPT violation, KVM walks one of two EPTs based on faulted GPA. Because it's costly to access secure EPT during walking EPTs with SEAMCALLs for the private guest physical address, another private EPT is used as a shadow of Secure-EPT with the existing logic at the cost of extra memory.The following depicts the relationship.
KVM | TDX module | | | -------------+---------- | | | | | | V V | | shared GPA private GPA | | CPU shared EPT pointer KVM private EPT pointer | CPU secure EPT pointer | | | | | | | | V V | V shared EPT private EPT<-------mirror----->Secure EPT | | | | | \--------------------+------\ | | | | | V | V V shared guest page | private guest page | | non-encrypted memory | encrypted memory |- Operating on Secure EPT
Use the TDX module APIs to operate on Secure EPT. To call the TDX API during resolving EPT violation, add hooks to additional operation and wiring it to TDX backend.* References
[1] TDX specification
https://software.intel.com/content/www/us/en/develop/articles/intel-trust-domain-extensions.html
[2] Intel Trust Domain Extensions (Intel TDX)
https://software.intel.com/content/dam/develop/external/us/en/documents/tdx-whitepaper-final9-17.pdf
[3] Intel CPU Architectural Extensions Specification
https://software.intel.com/content/dam/develop/external/us/en/documents-tps/intel-tdx-cpu-architectural-specification.pdf
[4] Intel TDX Module 1.0 EAShttps://software.intel.com/content/dam/develop/external/us/en/documents/tdx-module-1eas-v0.85.039.pdf[5] Intel TDX Loader Interface Specification
https://software.intel.com/content/dam/develop/external/us/en/documents-tps/intel-tdx-seamldr-interface-specification.pdf
[6] Intel TDX Guest-Hypervisor Communication Interface
https://software.intel.com/content/dam/develop/external/us/en/documents/intel-tdx-guest-hypervisor-communication-interface.pdf
[7] Intel TDX Virtual Firmware Design Guide
https://software.intel.com/content/dam/develop/external/us/en/documents/tdx-virtual-firmware-design-guide-rev-1.pdf
[8] intel public github
kvm TDX branch: https://github.com/intel/tdx/tree/kvm
TDX guest branch: https://github.com/intel/tdx/tree/guest
qemu TDX https://github.com/intel/qemu-tdx
[9] TDVF
https://github.com/tianocore/edk2-staging/tree/TDVFChao Gao (1):
KVM: x86: Allow to update cached values inkvm_user_return_msrsw/o
wrmsrIsaku Yamahata (73):
x86/virt/tdx: exportplatform_has_tdx
KVM: TDX: Detect CPU feature on kernel module initialization
KVM: x86: Refactor KVM VMX module init/exit functions
KVM: TDX: Add placeholders for TDX VM/vcpu structure x86/virt/tdx: Add a helper function to return system wide info about
TDX module
KVM: TDX: Add a function to initialize TDX module
KVM: TDX: Make TDX VM type supported
[MARKER] The start of TDX KVM patch series: TDX architectural
definitions
KVM: TDX: Define TDX architectural definitions
KVM: TDX: Add a function for KVM to invoke SEAMCALL
KVM: TDX: add a helper function for KVM to issue SEAMCALL
KVM: TDX: Add helper functions to print TDX SEAMCALL error [MARKER] The start of TDX KVM patch series: TD VM creation/destruction
KVM: TDX: allocate per-package mutex
x86/cpu: Add helper functions to allocate/free MKTME keyid
KVM: TDX: Add place holder for TDX VM specificmem_enc_opioctl
KVM: TDX: x86: Add vm ioctl to get TDX systemwide parameters [MARKER] The start of TDX KVM patch series: TD vcpu
creation/destruction
KVM: TDX: allocate/free TDX vcpu structure
[MARKER] The start of TDX KVM patch series: KVM MMU GPA stolen bits
KVM: x86/mmu: introduce config for PRIVATE KVM MMU [MARKER] The start of TDX KVM patch series: KVM TDP refactoring for
TDX
KVM: x86/mmu: Disallow fast page fault on private GPA [MARKER] The start of TDX KVM patch series: KVM TDP MMU hooks
KVM: x86/tdp_mmu: makeREMOVED_SPTEincludeshadow_initialvalue
KVM: x86/tdp_mmu: refactorkvm_tdp_mmu_map()
KVM: x86/mmu: add a private pointer to structkvm_mmu_page
KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
KVM: x86/tdp_mmu: Ignore unsupported mmu operation on private GFNs [MARKER] The start of TDX KVM patch series: TDX EPT violation
KVM: TDX: TDP MMU TDX support
[MARKER] The start of TDX KVM patch series: KVM TDP MMU MapGPA
KVM: x86/mmu: steal software usable bit for EPT to represent shared
page
KVM: x86/tdp_mmu: KeepPRIVATE_PROHIBITbit when zapping
KVM: x86/tdp_mmu: prevent private/shared map based onPRIVATE_PROHIBIT
KVM: x86/tdp_mmu: implement MapGPA hypercall for TDX
KVM: x86/mmu: Focibly use TDP MMU for TDX
[MARKER] The start of TDX KVM patch series: TD finalization
KVM: TDX: Create initial guest memory
KVM: TDX: Finalize VM initialization
[MARKER] The start of TDX KVM patch series: TD vcpu enter/exit
KVM: TDX: Add helper assembly function to TDX vcpu
KVM: TDX: Implement TDX vcpu enter/exit path
KVM: TDX:vcpu_run: save/restore host state(host kernel gs)
KVM: TDX: restore host xsave state when exit from the guest TD
KVM: TDX: restore user ret MSRs
[MARKER] The start of TDX KVM patch series: TD vcpu
exits/interrupts/hypercalls
KVM: TDX: complete interrupts after tdexit
KVM: TDX: restore debug store when TD exit
KVM: TDX: handle vcpu migration over logical processor
KVM: TDX: track LP tdx vcpu run and teardown vcpus on descroing the
guest TD
KVM: x86: Add aswitch_db_regsflag to handle TDX's auto-switched
behavior
KVM: TDX: Implement interrupt injection
KVM: TDX: Implements vcpurequest_immediate_exit
KVM: TDX: Implement methods to inject NMI
KVM: TDX: Add a place holder to handle TDX VM exit
KVM: TDX: handleEXIT_REASON_OTHER_SMI
KVM: TDX: handle ept violation/misconfig exit
KVM: TDX: handleEXCEPTION_NMIandEXTERNAL_INTERRUPT
KVM: TDX: Add TDG.VP.VMCALL accessors to access guest vcpu registers
KVM: TDX: handle KVM hypercall with TDG.VP.VMCALL
KVM: TDX: Handle TDX PV CPUID hypercall
KVM: TDX: Handle TDX PV HLT hypercall
KVM: TDX: Handle TDX PV port io hypercall
KVM: TDX: Implement callbacks for MSR operations for TDX
KVM: TDX: Handle TDX PV rdmsr hypercall
KVM: TDX: Handle TDX PV wrmsr hypercall
KVM: TDX: Handle TDX PV report fatal error hypercall
KVM: TDX: Handle TDX PVmap_gpahypercall
KVM: TDX: Silently discard SMI request
KVM: TDX: Silently ignore INIT/SIPIDocumentation/virtual/kvm: Document on Trust Domain Extensions(TDX)
KVM: x86: design documentation on TDX support of x86 KVM TDP MMUKai Huang (1):
KVM: x86: Introduce hooks to free VM callback prezap andvm_freeRick Edgecombe (1):
KVM: x86: Add infrastructure for stolen GPA bitsSean Christopherson (26):
KVM: VMX: Move outvmx_x86_opsto 'main.c' to wrap VMX and TDX
KVM: Enable hardware before doing arch VM initialization
KVM: x86: Introducevm_typeto differentiate default VMs from
confidential VMs
KVM: TDX: Add TDX "architectural" error codes
KVM: TDX: Add C wrapper functions for SEAMCALLs to the TDX module
KVM: TDX: Stub intdx.hwith structs, accessors, and VMCS helpers
KVM: Addmax_vcpusfield in common 'struct kvm'
KVM: TDX: create/destroy VM structure
KVM: TDX: Do TDX specific vcpu initialization
KVM: x86/mmu: Disallow dirty logging for x86 TDX
KVM: x86/mmu: Explicitly check for MMIO spte in fast page fault
KVM: x86/mmu: Allow non-zero init value for shadow PTE
KVM: x86/mmu: Allow per-VM override of the TDP max page level
KVM: VMX: Split out guts of EPT violation to common/exposed function
KVM: VMX: Move setting of EPT MMU masks to common VT-x code
KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
KVM: TDX: Addload_mmu_pgdmethod for TDX
KVM: x86/mmu: Introducekvm_mmu_map_tdp_page()for use by TDX
KVM: x86: Check for pending APICv interrupt inkvm_vcpu_has_events()
KVM: x86: Add option to force LAPIC expiration wait
KVM: VMX: Modify NMI and INTR handlers to takeintr_infoas function
argument
KVM: VMX: Move NMI/exception handler to common helper
KVM: x86: Split core of hypercall emulation to helper function
KVM: TDX: Add a placeholder for handler of TDX hypercalls(TDG.VP.VMCALL)KVM: TDX: Handle TDX PV MMIO hypercall
KVM: TDX: Add methods to ignore accesses to CPU stateXiaoyao Li (1):
KVM: TDX: initialize VM with TDX specific parametersYuan Yao (1):
KVM: TDX: Usevcpu_to_pi_desc()uniformly inposted_intr.cDocumentation/virt/kvm/api.rst | 24 +- .../virt/kvm/intel-tdx-layer-status.rst | 33 + Documentation/virt/kvm/intel-tdx.rst | 360 +++ Documentation/virt/kvm/tdx-tdp-mmu.rst | 466 ++++ arch/arm64/include/asm/kvm_host.h | 3 - arch/arm64/kvm/arm.c | 6 +- arch/arm64/kvm/vgic/vgic-init.c | 6 +- arch/x86/events/intel/ds.c | 1 + arch/x86/include/asm/kvm-x86-ops.h | 5 + arch/x86/include/asm/kvm_host.h | 38 +- arch/x86/include/asm/tdx.h | 61 + arch/x86/include/asm/vmx.h | 2 + arch/x86/include/uapi/asm/kvm.h | 59 + arch/x86/include/uapi/asm/vmx.h | 5 +- arch/x86/kvm/Kconfig | 4 + arch/x86/kvm/Makefile | 3 +- arch/x86/kvm/lapic.c | 25 +- arch/x86/kvm/lapic.h | 2 +- arch/x86/kvm/mmu.h | 65 +- arch/x86/kvm/mmu/mmu.c | 232 +- arch/x86/kvm/mmu/mmu_internal.h | 84 + arch/x86/kvm/mmu/paging_tmpl.h | 25 +- arch/x86/kvm/mmu/spte.c | 48 +- arch/x86/kvm/mmu/spte.h | 40 +- arch/x86/kvm/mmu/tdp_iter.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 642 ++++- arch/x86/kvm/mmu/tdp_mmu.h | 16 +- arch/x86/kvm/svm/svm.c | 10 +- arch/x86/kvm/vmx/common.h | 155 ++ arch/x86/kvm/vmx/main.c | 1026 ++++++++ arch/x86/kvm/vmx/posted_intr.c | 8 +- arch/x86/kvm/vmx/seamcall.S | 55 + arch/x86/kvm/vmx/seamcall.h | 25 + arch/x86/kvm/vmx/tdx.c | 2337 +++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 253 ++ arch/x86/kvm/vmx/tdx_arch.h | 158 ++ arch/x86/kvm/vmx/tdx_errno.h | 29 + arch/x86/kvm/vmx/tdx_error.c | 22 + arch/x86/kvm/vmx/tdx_ops.h | 174 ++ arch/x86/kvm/vmx/vmenter.S | 146 + arch/x86/kvm/vmx/vmx.c | 619 ++--- arch/x86/kvm/vmx/x86_ops.h | 235 ++ arch/x86/kvm/x86.c | 123 +- arch/x86/kvm/x86.h | 8 + arch/x86/virt/tdxcall.S | 8 +- arch/x86/virt/vmx/tdx.c | 50 +- arch/x86/virt/vmx/tdx.h | 52 - include/linux/kvm_host.h | 2 + include/uapi/linux/kvm.h | 1 + tools/arch/x86/include/uapi/asm/kvm.h | 59 + tools/include/uapi/linux/kvm.h | 1 + virt/kvm/kvm_main.c | 35 +- 52 files changed, 7142 insertions(+), 706 deletions(-)create mode 100644
Documentation/virt/kvm/intel-tdx-layer-status.rstcreate mode 100644Documentation/virt/kvm/intel-tdx.rstcreate mode 100644Documentation/virt/kvm/tdx-tdp-mmu.rstcreate mode 100644arch/x86/kvm/vmx/common.h
create mode 100644arch/x86/kvm/vmx/main.c
create mode 100644arch/x86/kvm/vmx/seamcall.S
create mode 100644arch/x86/kvm/vmx/seamcall.h
create mode 100644arch/x86/kvm/vmx/tdx.c
create mode 100644arch/x86/kvm/vmx/tdx.h
create mode 100644arch/x86/kvm/vmx/tdx_arch.h
create mode 100644arch/x86/kvm/vmx/tdx_errno.h
create mode 100644arch/x86/kvm/vmx/tdx_error.c
create mode 100644arch/x86/kvm/vmx/tdx_ops.h
create mode 100644arch/x86/kvm/vmx/x86_ops.h
Paolo Bonzini
maintainerRe: [RFC PATCH v5 053/104] KVM: x86/mmu: steal software usable bit for EPT to represent shared page
In reply to [email protected]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
Paolo Bonzini
maintainerRe: [RFC PATCH v5 017/104] KVM: TDX: Add helper functions to print TDX SEAMCALL error
In reply to [email protected]Quoted 5 lines · On 3/4/22 20:48, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Add helper functions to print out errors from the TDX module in a uniform manner.
Quoted 7 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/vmx/seamcall.h | 2 ++ arch/x86/kvm/vmx/tdx_error.c | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-)create mode 100644
arch/x86/kvm/vmx/tdx_error.c
When rebasing against tip/x86/tdx, the new .c file needs to include asm/tdx.h.
Paolo
Quoted 54 lines
arch/x86/kvm/Makefile +1
-1@@ -24,7 +24,7 @@ kvm-$(CONFIG_KVM_XEN) += xen.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ vmx/evmcs.o vmx/nested.o vmx/posted_intr.o vmx/main.o kvm-intel-$(CONFIG_X86_SGX_KVM) += vmx/sgx.o -kvm-intel-$(CONFIG_INTEL_TDX_HOST) += vmx/tdx.o vmx/seamcall.o +kvm-intel-$(CONFIG_INTEL_TDX_HOST) += vmx/tdx.o vmx/seamcall.o vmx/tdx_error.o kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o svm/sev.oarch/x86/kvm/vmx/seamcall.h +2
-0@@ -16,6 +16,8 @@ struct tdx_module_output;u64 kvm_seamcall (u64 op,u64 rcx,u64 rdx,u64 r8,u64 r9,u64 r10,struct tdx_module_output *out); +void pr_tdx_error (u64 op,u64 error_code,const struct tdx_module_output *out); +#endif /* !__ASSEMBLY__ */ #endif /* CONFIG_INTEL_TDX_HOST */ arch/x86/kvm/vmx/tdx_error.c new file +22
-0@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0 +/* functions to record TDX SEAMCALL error */ + +#include <linux/kernel.h> +#include <linux/bug.h> + +#include "tdx_ops.h" + +void pr_tdx_error (u64 op,u64 error_code,const struct tdx_module_output *out) +{ +if (!out) { +pr_err_ratelimited ("SEAMCALL[%lld] failed: 0x%llx\n" , + op, error_code); +return ; + } + +pr_err_ratelimited ( +"SEAMCALL[%lld] failed: 0x%llx " +"RCX 0x%llx, RDX 0x%llx, R8 0x%llx, R9 0x%llx, R10 0x%llx, R11 0x%llx\n" , + op, error_code, + out->rcx, out->rdx, out->r8, out->r9, out->r10, out->r11); +}
Paolo Bonzini
maintainerRe: [RFC PATCH v5 019/104] KVM: TDX: Stub in tdx.h with structs, accessors, and VMCS helpers
In reply to [email protected]Quoted 11 lines · On 3/4/22 20:48, [email protected] wrote:
From: Sean Christopherson <[email protected]>
Stub in
kvm_tdx,vcpu_tdx, and their various accessors. TDX defines SEAMCALL APIs to access TDX control structures corresponding to the VMX VMCS. Introduce helper accessors to hide its SEAMCALL ABI details.Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.h | 101 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+)
When rebasing against tip/x86/tdx, the new .h file needs to include asm/tdx.h.
Paolo
Quoted 127 lines
arch/x86/kvm/vmx/tdx.h +101
-0@@ -3,14 +3,29 @@#define __KVM_X86_TDX_H#ifdef CONFIG_INTEL_TDX_HOST + +#include "tdx_ops.h" +int tdx_module_setup (void ); +struct tdx_td_page { +unsigned long va; +hpa_t pa; +bool added; +}; +struct kvm_tdx {struct kvm kvm; + +struct tdx_td_page tdr; +struct tdx_td_page *tdcs; };struct vcpu_tdx {struct kvm_vcpu vcpu; + +struct tdx_td_page tdvpr; +struct tdx_td_page *tdvpx; };static inline bool is_td (struct kvm *kvm)@@ -32,6 +47,92 @@ static inline struct vcpu_tdx *to_tdx(struct kvm_vcpu *vcpu) { return container_of(vcpu, struct vcpu_tdx, vcpu); } + +static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) +{ +BUILD_BUG_ON_MSG (__builtin_constant_p (field) && (field) &0x1 , +"Read/Write to TD VMCS *_HIGH fields not supported" ); + +BUILD_BUG_ON (bits !=16 && bits !=32 && bits !=64 ); + +BUILD_BUG_ON_MSG (bits !=64 &&__builtin_constant_p (field) && + (((field) &0x6000 ) ==0x2000 || + ((field) &0x6000 ) ==0x6000 ), +"Invalid TD VMCS access for 64-bit field" ); +BUILD_BUG_ON_MSG (bits !=32 &&__builtin_constant_p (field) && + ((field) &0x6000 ) ==0x4000 , +"Invalid TD VMCS access for 32-bit field" ); +BUILD_BUG_ON_MSG (bits !=16 &&__builtin_constant_p (field) && + ((field) &0x6000 ) ==0x0000 , +"Invalid TD VMCS access for 16-bit field" ); +} + +static __always_inline void tdvps_state_non_arch_check(u64 field, u8 bits) {} +static __always_inline void tdvps_management_check(u64 field, u8 bits) {} + +#define TDX_BUILD_TDVPS_ACCESSORS (bits, uclass, lclass) \ +static __always_inline u##bits td_##lclass##_read##bits(struct vcpu_tdx *tdx, \ + u32 field) \ +{ \ + struct tdx_module_output out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_rd(tdx->tdvpr.pa, TDVPS_##uclass(field), &out); \ + if (unlikely(err)) { \ + pr_err("TDH_VP_RD["#uclass".0x%x] failed: 0x%llx\n", \ + field, err); \ + return 0; \ + } \ + return (u##bits)out.r8; \ +} \ +static __always_inline void td_##lclass##_write##bits(struct vcpu_tdx *tdx, \ + u32 field, u##bits val) \ +{ \ + struct tdx_module_output out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr.pa, TDVPS_##uclass(field), val, \ + GENMASK_ULL(bits - 1, 0), &out); \ + if (unlikely(err)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] = 0x%llx failed: 0x%llx\n", \ + field, (u64)val, err); \ +} \ +static __always_inline void td_##lclass##_setbit##bits(struct vcpu_tdx *tdx, \ + u32 field, u64 bit) \ +{ \ + struct tdx_module_output out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr.pa, TDVPS_##uclass(field), bit, bit, \ + &out); \ + if (unlikely(err)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] |= 0x%llx failed: 0x%llx\n", \ + field, bit, err); \ +} \ +static __always_inline void td_##lclass##_clearbit##bits(struct vcpu_tdx *tdx, \ + u32 field, u64 bit) \ +{ \ + struct tdx_module_output out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr.pa, TDVPS_##uclass(field), 0, bit, \ + &out); \ + if (unlikely(err)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] &= ~0x%llx failed: 0x%llx\n", \ + field, bit, err); \ +} + +TDX_BUILD_TDVPS_ACCESSORS (16 ,VMCS , vmcs); +TDX_BUILD_TDVPS_ACCESSORS (32 ,VMCS , vmcs); +TDX_BUILD_TDVPS_ACCESSORS (64 ,VMCS , vmcs); + +TDX_BUILD_TDVPS_ACCESSORS (64 ,STATE_NON_ARCH , state_non_arch); +TDX_BUILD_TDVPS_ACCESSORS (8 ,MANAGEMENT , management); +#else static inline int tdx_module_setup (void ) {return -ENODEV ; };
Paolo Bonzini
maintainerQuoted 13 lines · Paolo Bonzini
Quoted 11 lines · On 3/4/22 20:48, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Hi. Now TDX host kernel patch series was posted, I've rebased this patch series to it and make it work.
https://lore.kernel.org/lkml/[email protected]/
Changes from v4:
- rebased to TDX host kernel patch series.
- include all the patches to make this patch series working.
- add [MARKER] patches to mark the patch layer clear.
Quoted 5 lines
I think I have reviewed everything except the TDP MMU parts (48, 54-57).
I will do those next week, but in the meanwhile feel free to send v6
if you have it ready. A lot of the requests have been cosmetic.If you would like to use something like Trello to track all the changes, and submit before you have done all of them, that's fine by me.
Also, I have now pushed what (I think) should be all that's needed to run TDX guests at branch kvm-tdx-5.17 of https://git.kernel.org/pub/scm/virt/kvm/kvm.git. It's only compile-tested for now, but if I missed something please report so that it can be used by people doing other work (including QEMU, TDVF and guest).
Thanks,
Paolo
Quoted 638 lines
Paolo
Quoted 635 lines
Thanks,
* What's TDX?
TDX stands for Trust Domain Extensions, which extends Intel Virtual Machines Extensions (VMX) to introduce a kind of virtual machine guest called a Trust Domain (TD) for confidential computing.A TD runs in a CPU mode that is designed to protect the confidentiality of its memory contents and its CPU state from any other software, including the hosting Virtual Machine Monitor (VMM), unless explicitly shared by the TD itself.
We have more detailed explanations below (***). We have the high-level design of TDX KVM below (****).
In this patch series, we use "TD" or "guest TD" to differentiate it from the current "VM" (Virtual Machine), which is supported by KVM today.
* The organization of this patch series
This patch series is on top of the patches series "TDX host kernel support":
https://lore.kernel.org/lkml/[email protected]/this patch series is available at
https://github.com/intel/tdx/releases/tag/kvm-upstream
The corresponding patches to qemu are available at
https://github.com/intel/qemu-tdx/commits/tdx-upstreamThe relations of the layers are depicted as follows.
The arrows below show the order of patch reviews we would like to have.The below layers are chosen so that the device model, for example, qemu can exercise each layering step by step. Check if TDX is supported, create TD VM, create TD vcpu, allow vcpu running, populate TD guest private memory, and handle vcpu exits/hypercalls/interrupts to run TD fully.
TDX vcpu
interrupt/exits/hypercall<------------\
^ || | TD finalization | ^ | | | TDX EPT violation<------------\ | ^ | | | | | TD vcpu enter/exit | | ^ | | | | | TD vcpu creation/destruction | \-------KVM TDP MMU MapGPA ^ | ^ | | |TD VM creation/destruction \---------------KVM TDP MMU hooks ^ ^
| |
TDX architectural definitions KVM TDP refactoring for TDX ^ ^
| |
TDX, VMX <--------TDX host kernel KVM MMU GPA stolen bits coexistence supportThe followings are explanations of each layer. Each layer has a dummy commit that starts with [MARKER] in subject. It is intended to help to identify where each layer starts.
TDX host kernel support:
https://lore.kernel.org/lkml/[email protected]/
The guts of system-wide initialization of TDX module. There is an independent patch series for host x86. TDX KVM patches call functions this patch series provides to initialize the TDX module.TDX, VMX coexistence:
Infrastructure to allow TDX to coexist with VMX and trigger the initialization of the TDX module.
This layer starts with
"KVM: VMX: Move outvmx_x86_opsto 'main.c' to wrap VMX and TDX" TDX architectural definitions:
Add TDX architectural definitions and helper functions
This layer starts with
"[MARKER] The start of TDX KVM patch series: TDX architectural definitions". TD VM creation/destruction:
Guest TD creation/destroy allocation and releasing of TDX specific vm and vcpu structure. Create an initial guest memory image with TDX measurement.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD VM creation/destruction". TD vcpu creation/destruction:
guest TD creation/destroy Allocation and releasing of TDX specific vm and vcpu structure. Create an initial guest memory image with TDX measurement.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD vcpu creation/destruction" TDX EPT violation:
Create an initial guest memory image with TDX measurement. Handle secure EPT violations to populate guest pages with TDX SEAMCALLs. This layer starts with
"[MARKER] The start of TDX KVM patch series: TDX EPT violation"
TD vcpu enter/exit:
Allow TDX vcpu to enter into TD and exit from TD. Save CPU state before entering into TD. Restore CPU state after exiting from TD.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD vcpu enter/exit" TD vcpu interrupts/exit/hypercall:
Handle various exits/hypercalls and allow interrupts to be injected so that TD vcpu can continue running.
This layer starts with
"[MARKER] The start of TDX KVM patch series: TD vcpu exits/interrupts/hypercalls"KVM MMU GPA stolen bits:
Introduce framework to handle stolen repurposed bit of GPA TDX repurposed a bit of GPA to indicate shared or private. If it's shared, it's the same as the conventional VMX EPT case. VMM can access shared guest pages. If it's private, it's handled by Secure-EPT and the guest page is encrypted.
This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM MMU GPA stolen bits" KVM TDP refactoring for TDX:
TDX Secure EPT requires different constants. e.g. initial value EPT entry value etc. Various refactoring for those differences. This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM TDP refactoring for TDX" KVM TDP MMU hooks:
Introduce framework to TDP MMU to add hooks in addition to direct EPT access TDX added Secure EPT which is an enhancement to VMX EPT. Unlike conventional VMX EPT, CPU can't directly read/write Secure EPT. Instead, use TDX SEAMCALLs to operate on Secure EPT.
This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM TDP MMU hooks" KVM TDP MMU MapGPA:
Introduce framework to handle switching guest pages from private/shared to shared/private. For a given GPA, a guest page can be assigned to a private GPA or a shared GPA exclusively. With TDX MapGPA hypercall, guest TD converts GPA assignments from private (or shared) to shared (or private).
This layer starts with
"[MARKER] The start of TDX KVM patch series: KVM TDP MMU MapGPA "KVM guest private memory: (not shown in the above diagram)
[PATCH v4 00/12] KVM: mm: fd-based approach for supporting KVM guest private
memory: https://lkml.org/lkml/2022/1/18/395
Guest private memory requires different memory management in KVM. The patch proposes a way for it. Integration with TDX KVM.(***)
* TDX module
A CPU-attested software module called the "TDX module" is designed to implement the TDX architecture, and it is loaded by the UEFI firmware today. It can be loaded by the kernel or driver at runtime, but in this patch series we assume that the TDX module is already loaded and initialized.The TDX module provides two main new logical modes of operation built upon the new SEAM (Secure Arbitration Mode) root and non-root CPU modes added to the VMX architecture. TDX root mode is mostly identical to the VMX root operation mode, and the TDX functions (described later) are triggered by the new SEAMCALL instruction with the desired interface function selected by an input operand (leaf number, in RAX). TDX non-root mode is used for TD guest operation. TDX non-root operation (i.e. "guest TD" mode) is similar to the VMX non-root operation (i.e. guest VM), with changes and restrictions to better assure that no other software or hardware has direct visibility of the TD memory and state.
TDX transitions between TDX root operation and TDX non-root operation include TD Entries, from TDX root to TDX non-root mode, and TD Exits from TDX non-root to TDX root mode. A TD Exit might be asynchronous, triggered by some external event (e.g., external interrupt or SMI) or an exception, or it might be synchronous, triggered by a TDCALL (TDG.VP.VMCALL) function.
TD VCPUs can be entered using SEAMCALL(TDH.VP.ENTER) by KVM. TDH.VP.ENTER is one of the TDX interface functions as mentioned above, and "TDH" stands for Trust Domain Host. Those host-side TDX interface functions are categorized into various areas just for better organization, such as SYS (TDX module management), MNG (TD management), VP (VCPU), PHYSMEM (physical memory), MEM (private memory), etc. For example, SEAMCALL(TDH.SYS.INFO) returns the TDX module information.
TDCS (Trust Domain Control Structure) is the main control structure of a guest TD, and encrypted (using the guest TD's ephemeral private key). At a high level, TDCS holds information for controlling TD operation as a whole, execution, EPTP, MSR bitmaps, etc that KVM needs to set it up. Note that MSR bitmaps are held as part of TDCS (unlike VMX) because they are meant to have the same value for all VCPUs of the same TD.
Trust Domain Virtual Processor State (TDVPS) is the root control structure of a TD VCPU. It helps the TDX module control the operation of the VCPU, and holds the VCPU state while the VCPU is not running. TDVPS is opaque to software and DMA access, accessible only by using the TDX module interface functions (such as TDH.VP.RD, TDH.VP.WR). TDVPS includes TD VMCS, and TD VMCS auxiliary structures, such as virtual APIC page, virtualization exception information, etc.
Several VMX control structures (such as Shared EPT and Posted interrupt descriptor) are directly managed and accessed by the host VMM. These control structures are pointed to by fields in the TD VMCS.
The above means that 1) KVM needs to allocate different data structures for TDs,
2) KVM can reuse the existing code for TDs for some operations, 3) it needs to define TD-specific handling for others. 3) Redirect operations to . 3) Redirect operations to the TDX specific callbacks, like "if (is_td_vcpu(vcpu))tdx_callback()elsevmx_callback();".*TD Private Memory
TD private memory is designed to hold TD private content, encrypted by the CPU using the TD ephemeral key. An encryption engine holds a table of encryption keys, and an encryption key is selected for each memory transaction based on a Host Key Identifier (HKID). By design, the host VMM does not have access to the encryption keys.In the first generation of MKTME, HKID is "stolen" from the physical address by allocating a configurable number of bits from the top of the physical address. The HKID space is partitioned into shared HKIDs for legacy MKTME accesses and private HKIDs for SEAM-mode-only accesses. We use 0 for the shared HKID on the host so that MKTME can be opaque or bypassed on the host.
During TDX non-root operation (i.e. guest TD), memory accesses can be qualified as either shared or private, based on the value of a new SHARED bit in the Guest Physical Address (GPA). The CPU translates shared GPAs using the usual VMX EPT (Extended Page Table) or "Shared EPT" (in this document), which resides in host VMM memory. The Shared EPT is directly managed by the host VMM - the same as with the current VMX. Since guest TDs usually require I/O, and the data exchange needs to be done via shared memory, thus KVM needs to use the current EPT functionality even for TDs.
* Secure EPT and Minoring using the TDP code
The CPU translates private GPAs using a separate Secure EPT. The Secure EPT pages are encrypted and integrity-protected with the TD's ephemeral private key. Secure EPT can be managed _indirectly_ by the host VMM, using the TDX interface functions, and thus conceptually Secure EPT is a subset of EPT (why "subset"). Since execution of such interface functions takes much longer time than accessing memory directly, in KVM we use the existing TDP code to minor the Secure EPT for the TD.This way, we can effectively walk Secure EPT without using the TDX interface functions.
* VM life cycle and TDX specific operations
The userspace VMM, such as QEMU, needs to build and treat TDs differently. For example, a TD needs to boot in private memory, and the host software cannot copy the initial image to private memory.* TSC Virtualization
The TDX module helps TDs maintain reliable TSC (Time Stamp Counter) values (e.g. consistent among the TD VCPUs) and the virtual TSC frequency is determined by TD configuration, i.e. when the TD is created, not per VCPU. The current KVM owns TSC virtualization for VMs, but the TDX module does for TDs.* MCE support for TDs
The TDX module doesn't allow VMM to inject MCE. Instead PV way is needed for TD to communicate with VMM. For now, KVM silently ignores MCE request by VMM. MSRs related to MCE (e.g, MCE bank registers) can be naturally emulated by paravirtualizing MSR access.[1] For details, the specifications, [2], [3], [4], [5], [6], [7], are available.
* Restrictions or future work
Some features are not included to reduce patch size. Those features are addressed as future independent patch series.
- large page (2M, 1G)
- qemu gdb stub
- guest PMU
- and more* Prerequisites
It's required to load the TDX module and initialize it. It's out of the scope of this patch series. Another independent patch for the common x86 code is planned. It definesCONFIG_INTEL_TDX_HOSTand this patch series uses CONFIG_INTEL_TDX_HOST. It's assumed that WithCONFIG_INTEL_TDX_HOST=y, the TDX module is initialized and ready for KVM to use the TDX module APIs for TDX guest life cycle like tdh.mng.init are ready to use.Concretely Global initialization, LP (Logical Processor) initialization, global configuration, the key configuration, and TDMR and PAMT initialization are done. The state of the TDX module is SYS_READY. Please refer to the TDX module specification, the chapter Intel TDX Module Lifecycle State Machine
** Detecting the TDX module readiness.
TDX host patch series implements the detection of the TDX module availability and its initialization so that KVM can use it. Also it manages Host KeyID (HKID) assigned to guest TD.
The assumed APIs the TDX host patch series provides are
- intseamrr_enabled()
Check if required cpu feature (SEAM mode) is available. This only check CPU feature availability. At this point, the TDX module may not be ready for KVM to use.
- intinit_tdx(void);
Initialization of TDX module so that the TDX module is ready for KVM to use.
- const structtdsysinfo_struct*tdx_get_sysinfo(void);
Return the system wide information about the TDX module. NULL if the TDX isn't initialized.
- u32tdx_get_global_keyid(void);
Return global key id that is used for the TDX module itself.
- inttdx_keyid_alloc(void);
Allocate HKID for guest TD.
- voidtdx_keyid_free(int keyid);
Free HKID for guest TD.(****)
* TDX KVM high-level design
- Host key ID management
Host Key ID (HKID) needs to be assigned to each TDX guest for memory encryption. It is assumed The TDX host patch series implements necessary functions, u32tdx_get_global_keyid(void), inttdx_keyid_alloc(void)and,
voidtdx_keyid_free(int keyid).- Data structures and VM type
Because TDX is different from VMX, define its own VM/VCPU structures, structkvm_tdxand structvcpu_tdxinstead of structkvm_vmxand struct vcpu_vmx. To identify the VM, introduce VM-type to specify which VM type, VMX (default) or TDX, is used.- VM life cycle and TDX specific operations
Re-purpose the existingKVM_MEMORY_ENCRYPT_OPto add TDX specific operations. New commands are used to get the TDX system parameters, set TDX specific VM/VCPU parameters, set initial guest memory and measurement.The creation of TDX VM requires five additional operations in addition to the conventional VM creation.
- Get KVM system capability to check if TDX VM type is supported
- VM creation (KVM_CREATE_VM)
- New: Get the TDX specific system parameters.KVM_TDX_GET_CAPABILITY.
- New: Set TDX specific VM parameters.KVM_TDX_INIT_VM.
- VCPU creation (KVM_CREATE_VCPU)
- New: Set TDX specific VCPU parameters.KVM_TDX_INIT_VCPU.
- New: Initialize guest memory as boot state and extend the measurement with the memory.KVM_TDX_INIT_MEM_REGION.
- New: Finalize VM.KVM_TDX_FINALIZE. Complete measurement of the initial TDX VM contents.
- VCPU RUN (KVM_VCPU_RUN)- Protected guest state
Because the guest state (CPU state and guest memory) is protected, the KVM VMM can't operate on them. For example, accessing CPU registers, injecting exceptions, and accessing guest memory. Those operations are handled as silently ignored, returning zero or initial reset value when it's requested via KVM API ioctls.VM/VCPU state and callbacks for TDX specific operations. Define tdx specific VM state and VCPU state instead of VMX ones. Redirect operations to TDX specific callbacks. "if (tdx)
tdx_op()elsevmx_op()".Operations on the CPU state
silently ignore operations on the guest state. For example, the write to CPU registers is ignored and the read from CPU registers returns 0.. ignore access to CPU registers except for allowed ones.
. TSC: add a check if tsc is immutable and return an error. Because the KVM implementation updates the internal tsc state and it's difficult to back out those changes. Instead, skip the logic.
. dirty logging: add check if dirty logging is supported.
. exceptions/SMI/MCE/SIPI/INIT: silently ignoreNote: virtual external interrupt and NMI can be injected into TDX guests.
- KVM MMU integration
One bit of the guest physical address (bit 51 or 47) is repurposed to indicate if the guest physical address is private (the bit is cleared) or shared (the bit is set). The bits are called stolen bits.- Stolen bits framework
systematically tracks which guest physical address, shared or private, is used.- Shared EPT and secure EPT
There are two EPTs. Shared EPT (the conventional one) and Secure EPT(the new one). Shared EPT is handled the same for the stolen bit set. Secure EPT points to private guest pages. To resolve EPT violation, KVM walks one of two EPTs based on faulted GPA. Because it's costly to access secure EPT during walking EPTs with SEAMCALLs for the private guest physical address, another private EPT is used as a shadow of Secure-EPT with the existing logic at the cost of extra memory.The following depicts the relationship.
KVM | TDX module
| | | -------------+---------- | | | | | | V V | | shared GPA private GPA | | CPU shared EPT pointer KVM private EPT pointer | CPU secure EPT pointer | | | | | | | | V V | V shared EPT private EPT<-------mirror----->Secure EPT | | | | | \--------------------+------\ | | | | | V | V V shared guest page | private guest page | | non-encrypted memory | encrypted memory |- Operating on Secure EPT
Use the TDX module APIs to operate on Secure EPT. To call the TDX API during resolving EPT violation, add hooks to additional operation and wiring it to TDX backend.* References
[1] TDX specification
https://software.intel.com/content/www/us/en/develop/articles/intel-trust-domain-extensions.html
[2] Intel Trust Domain Extensions (Intel TDX)
https://software.intel.com/content/dam/develop/external/us/en/documents/tdx-whitepaper-final9-17.pdf
[3] Intel CPU Architectural Extensions Specification
[4] Intel TDX Module 1.0 EAS
[5] Intel TDX Loader Interface Specification
[6] Intel TDX Guest-Hypervisor Communication Interface
[7] Intel TDX Virtual Firmware Design Guide
[8] intel public github
kvm TDX branch: https://github.com/intel/tdx/tree/kvm
TDX guest branch: https://github.com/intel/tdx/tree/guest
qemu TDX https://github.com/intel/qemu-tdx
[9] TDVF
https://github.com/tianocore/edk2-staging/tree/TDVFChao Gao (1):
KVM: x86: Allow to update cached values inkvm_user_return_msrsw/o wrmsrIsaku Yamahata (73):
x86/virt/tdx: exportplatform_has_tdx
KVM: TDX: Detect CPU feature on kernel module initialization
KVM: x86: Refactor KVM VMX module init/exit functions
KVM: TDX: Add placeholders for TDX VM/vcpu structure x86/virt/tdx: Add a helper function to return system wide info about TDX module
KVM: TDX: Add a function to initialize TDX module
KVM: TDX: Make TDX VM type supported
[MARKER] The start of TDX KVM patch series: TDX architectural definitions
KVM: TDX: Define TDX architectural definitions
KVM: TDX: Add a function for KVM to invoke SEAMCALL
KVM: TDX: add a helper function for KVM to issue SEAMCALL
KVM: TDX: Add helper functions to print TDX SEAMCALL error [MARKER] The start of TDX KVM patch series: TD VM creation/destruction
KVM: TDX: allocate per-package mutex
x86/cpu: Add helper functions to allocate/free MKTME keyid
KVM: TDX: Add place holder for TDX VM specificmem_enc_opioctl
KVM: TDX: x86: Add vm ioctl to get TDX systemwide parameters [MARKER] The start of TDX KVM patch series: TD vcpu creation/destruction
KVM: TDX: allocate/free TDX vcpu structure
[MARKER] The start of TDX KVM patch series: KVM MMU GPA stolen bits
KVM: x86/mmu: introduce config for PRIVATE KVM MMU [MARKER] The start of TDX KVM patch series: KVM TDP refactoring for TDX
KVM: x86/mmu: Disallow fast page fault on private GPA [MARKER] The start of TDX KVM patch series: KVM TDP MMU hooks
KVM: x86/tdp_mmu: makeREMOVED_SPTEincludeshadow_initialvalue
KVM: x86/tdp_mmu: refactorkvm_tdp_mmu_map()
KVM: x86/mmu: add a private pointer to structkvm_mmu_page
KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
KVM: x86/tdp_mmu: Ignore unsupported mmu operation on private GFNs [MARKER] The start of TDX KVM patch series: TDX EPT violation
KVM: TDX: TDP MMU TDX support
[MARKER] The start of TDX KVM patch series: KVM TDP MMU MapGPA
KVM: x86/mmu: steal software usable bit for EPT to represent shared page
KVM: x86/tdp_mmu: KeepPRIVATE_PROHIBITbit when zapping
KVM: x86/tdp_mmu: prevent private/shared map based onPRIVATE_PROHIBIT
KVM: x86/tdp_mmu: implement MapGPA hypercall for TDX
KVM: x86/mmu: Focibly use TDP MMU for TDX
[MARKER] The start of TDX KVM patch series: TD finalization
KVM: TDX: Create initial guest memory
KVM: TDX: Finalize VM initialization
[MARKER] The start of TDX KVM patch series: TD vcpu enter/exit
KVM: TDX: Add helper assembly function to TDX vcpu
KVM: TDX: Implement TDX vcpu enter/exit path
KVM: TDX:vcpu_run: save/restore host state(host kernel gs)
KVM: TDX: restore host xsave state when exit from the guest TD
KVM: TDX: restore user ret MSRs
[MARKER] The start of TDX KVM patch series: TD vcpu exits/interrupts/hypercalls
KVM: TDX: complete interrupts after tdexit
KVM: TDX: restore debug store when TD exit
KVM: TDX: handle vcpu migration over logical processor
KVM: TDX: track LP tdx vcpu run and teardown vcpus on descroing the guest TD
KVM: x86: Add aswitch_db_regsflag to handle TDX's auto-switched behavior
KVM: TDX: Implement interrupt injection
KVM: TDX: Implements vcpurequest_immediate_exit
KVM: TDX: Implement methods to inject NMI
KVM: TDX: Add a place holder to handle TDX VM exit
KVM: TDX: handleEXIT_REASON_OTHER_SMI
KVM: TDX: handle ept violation/misconfig exit
KVM: TDX: handleEXCEPTION_NMIandEXTERNAL_INTERRUPT
KVM: TDX: Add TDG.VP.VMCALL accessors to access guest vcpu registers
KVM: TDX: handle KVM hypercall with TDG.VP.VMCALL
KVM: TDX: Handle TDX PV CPUID hypercall
KVM: TDX: Handle TDX PV HLT hypercall
KVM: TDX: Handle TDX PV port io hypercall
KVM: TDX: Implement callbacks for MSR operations for TDX
KVM: TDX: Handle TDX PV rdmsr hypercall
KVM: TDX: Handle TDX PV wrmsr hypercall
KVM: TDX: Handle TDX PV report fatal error hypercall
KVM: TDX: Handle TDX PVmap_gpahypercall
KVM: TDX: Silently discard SMI request
KVM: TDX: Silently ignore INIT/SIPIDocumentation/virtual/kvm: Document on Trust Domain Extensions(TDX)
KVM: x86: design documentation on TDX support of x86 KVM TDP MMUKai Huang (1):
KVM: x86: Introduce hooks to free VM callback prezap andvm_freeRick Edgecombe (1):
KVM: x86: Add infrastructure for stolen GPA bitsSean Christopherson (26):
KVM: VMX: Move outvmx_x86_opsto 'main.c' to wrap VMX and TDX
KVM: Enable hardware before doing arch VM initialization
KVM: x86: Introducevm_typeto differentiate default VMs from confidential VMs
KVM: TDX: Add TDX "architectural" error codes
KVM: TDX: Add C wrapper functions for SEAMCALLs to the TDX module
KVM: TDX: Stub intdx.hwith structs, accessors, and VMCS helpers
KVM: Addmax_vcpusfield in common 'struct kvm'
KVM: TDX: create/destroy VM structure
KVM: TDX: Do TDX specific vcpu initialization
KVM: x86/mmu: Disallow dirty logging for x86 TDX
KVM: x86/mmu: Explicitly check for MMIO spte in fast page fault
KVM: x86/mmu: Allow non-zero init value for shadow PTE
KVM: x86/mmu: Allow per-VM override of the TDP max page level
KVM: VMX: Split out guts of EPT violation to common/exposed function
KVM: VMX: Move setting of EPT MMU masks to common VT-x code
KVM: x86/mmu: Track shadow MMIO value/mask on a per-VM basis
KVM: TDX: Addload_mmu_pgdmethod for TDX
KVM: x86/mmu: Introducekvm_mmu_map_tdp_page()for use by TDX
KVM: x86: Check for pending APICv interrupt inkvm_vcpu_has_events()
KVM: x86: Add option to force LAPIC expiration wait
KVM: VMX: Modify NMI and INTR handlers to takeintr_infoas function argument
KVM: VMX: Move NMI/exception handler to common helper
KVM: x86: Split core of hypercall emulation to helper function
KVM: TDX: Add a placeholder for handler of TDX hypercalls (TDG.VP.VMCALL)
KVM: TDX: Handle TDX PV MMIO hypercall
KVM: TDX: Add methods to ignore accesses to CPU stateXiaoyao Li (1):
KVM: TDX: initialize VM with TDX specific parametersYuan Yao (1):
KVM: TDX: Usevcpu_to_pi_desc()uniformly inposted_intr.cDocumentation/virt/kvm/api.rst | 24 +- .../virt/kvm/intel-tdx-layer-status.rst | 33 + Documentation/virt/kvm/intel-tdx.rst | 360 +++ Documentation/virt/kvm/tdx-tdp-mmu.rst | 466 ++++ arch/arm64/include/asm/kvm_host.h | 3 - arch/arm64/kvm/arm.c | 6 +- arch/arm64/kvm/vgic/vgic-init.c | 6 +- arch/x86/events/intel/ds.c | 1 + arch/x86/include/asm/kvm-x86-ops.h | 5 + arch/x86/include/asm/kvm_host.h | 38 +- arch/x86/include/asm/tdx.h | 61 + arch/x86/include/asm/vmx.h | 2 + arch/x86/include/uapi/asm/kvm.h | 59 + arch/x86/include/uapi/asm/vmx.h | 5 +- arch/x86/kvm/Kconfig | 4 + arch/x86/kvm/Makefile | 3 +- arch/x86/kvm/lapic.c | 25 +- arch/x86/kvm/lapic.h | 2 +- arch/x86/kvm/mmu.h | 65 +- arch/x86/kvm/mmu/mmu.c | 232 +- arch/x86/kvm/mmu/mmu_internal.h | 84 + arch/x86/kvm/mmu/paging_tmpl.h | 25 +- arch/x86/kvm/mmu/spte.c | 48 +- arch/x86/kvm/mmu/spte.h | 40 +- arch/x86/kvm/mmu/tdp_iter.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 642 ++++- arch/x86/kvm/mmu/tdp_mmu.h | 16 +- arch/x86/kvm/svm/svm.c | 10 +- arch/x86/kvm/vmx/common.h | 155 ++ arch/x86/kvm/vmx/main.c | 1026 ++++++++ arch/x86/kvm/vmx/posted_intr.c | 8 +- arch/x86/kvm/vmx/seamcall.S | 55 + arch/x86/kvm/vmx/seamcall.h | 25 + arch/x86/kvm/vmx/tdx.c | 2337 +++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 253 ++ arch/x86/kvm/vmx/tdx_arch.h | 158 ++ arch/x86/kvm/vmx/tdx_errno.h | 29 + arch/x86/kvm/vmx/tdx_error.c | 22 + arch/x86/kvm/vmx/tdx_ops.h | 174 ++ arch/x86/kvm/vmx/vmenter.S | 146 + arch/x86/kvm/vmx/vmx.c | 619 ++--- arch/x86/kvm/vmx/x86_ops.h | 235 ++ arch/x86/kvm/x86.c | 123 +- arch/x86/kvm/x86.h | 8 +
arch/x86/virt/tdxcall.S | 8 +- arch/x86/virt/vmx/tdx.c | 50 +- arch/x86/virt/vmx/tdx.h | 52 -include/linux/kvm_host.h | 2 + include/uapi/linux/kvm.h | 1 + tools/arch/x86/include/uapi/asm/kvm.h | 59 + tools/include/uapi/linux/kvm.h | 1 + virt/kvm/kvm_main.c | 35 +-
52 files changed, 7142 insertions(+), 706 deletions(-)create mode 100644
Documentation/virt/kvm/intel-tdx-layer-status.rstcreate mode 100644Documentation/virt/kvm/intel-tdx.rstcreate mode 100644Documentation/virt/kvm/tdx-tdp-mmu.rstcreate mode 100644arch/x86/kvm/vmx/common.h
create mode 100644arch/x86/kvm/vmx/main.c
create mode 100644arch/x86/kvm/vmx/seamcall.S
create mode 100644arch/x86/kvm/vmx/seamcall.h
create mode 100644arch/x86/kvm/vmx/tdx.c
create mode 100644arch/x86/kvm/vmx/tdx.h
create mode 100644arch/x86/kvm/vmx/tdx_arch.h
create mode 100644arch/x86/kvm/vmx/tdx_errno.h
create mode 100644arch/x86/kvm/vmx/tdx_error.c
create mode 100644arch/x86/kvm/vmx/tdx_ops.h
create mode 100644arch/x86/kvm/vmx/x86_ops.h
Quoted 8 lines · Paolo Bonzini
Quoted 7 lines · On 3/4/22 20:48, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Hi. Now TDX host kernel patch series was posted, I've rebased this patch series to it and make it work.
https://lore.kernel.org/lkml/[email protected]/
Quoted 8 lines
Quoted 4 lines
Changes from v4:
- rebased to TDX host kernel patch series.
- include all the patches to make this patch series working.
- add [MARKER] patches to mark the patch layer clear.I think I have reviewed everything except the TDP MMU parts (48, 54-57). I will do those next week, but in the meanwhile feel free to send v6 if you have it ready. A lot of the requests have been cosmetic.
Thank you so much. I'm updating patches now.
Quoted 2 lines
If you would like to use something like Trello to track all the changes, and submit before you have done all of them, that's fine by me.
Sure. I've created public trello board.
If you want to edit it, please let me know. I'll add you to the project member.
https://trello.com/kvmtdxreview
thanks,
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
In reply to Isaku YamahataSorry, 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 048/104] KVM: x86/tdp_mmu: Support TDX private mapping for TDP MMU
In reply to Kai HuangQuoted 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.