KVM TDX basic feature support
Subsystems (6)
PATCH v17 75/116 KVM: TDX: remove use of struct vcpu_vmx from posted_interrupt.c
In reply to [email protected]From: Isaku Yamahata <[email protected]>
As TDX will use posted_interrupt.c, the use of struct vcpu_vmx is a blocker. Because the members of struct pi_desc pi_desc and struct list_head pi_wakeup_list are only used in posted_interrupt.c, introduce common structure, struct vcpu_pi, make vcpu_vmx and vcpu_tdx has same layout in the top of structure.
To minimize the diff size, avoid code conversion like, vmx->pi_desc => vmx->common->pi_desc. Instead add compile time check if the layout is expected.
--- arch/x86/kvm/vmx/posted_intr.c | 41 ++++++++++++++++++++++++++-------- arch/x86/kvm/vmx/posted_intr.h | 11 +++++++++ arch/x86/kvm/vmx/tdx.c | 1 + arch/x86/kvm/vmx/tdx.h | 8 +++++++ arch/x86/kvm/vmx/vmx.h | 14 +++++++----- 5 files changed, 60 insertions(+), 15 deletions(-)
arch/x86/kvm/vmx/posted_intr.c +32 -9
@@ -11,6 +11,7 @@#include "posted_intr.h" #include "trace.h" #include "vmx.h" +#include "tdx.h" /* * Maintain a per-CPU list of vCPUs that need to be awakened by wakeup_handler()
@@ -31,9 +32,29 @@ static DEFINE_PER_CPU(struct list_head, wakeup_vcpus_on_cpu);*/ static DEFINE_PER_CPU(raw_spinlock_t, wakeup_vcpus_on_cpu_lock); +/* +* The layout of the head of struct vcpu_vmx and struct vcpu_tdx must match with +* struct vcpu_pi. +*/ +static_assert (offsetof(struct vcpu_pi , pi_desc) == + offsetof(struct vcpu_vmx , pi_desc)); +static_assert (offsetof(struct vcpu_pi , pi_wakeup_list) == + offsetof(struct vcpu_vmx , pi_wakeup_list)); +#ifdef CONFIG_INTEL_TDX_HOST +static_assert (offsetof(struct vcpu_pi , pi_desc) == + offsetof(struct vcpu_tdx , pi_desc)); +static_assert (offsetof(struct vcpu_pi , pi_wakeup_list) == + offsetof(struct vcpu_tdx , pi_wakeup_list)); +#endif + +static inline struct vcpu_pi *vcpu_to_pi (struct kvm_vcpu *vcpu) +{ +return (struct vcpu_pi *)vcpu; +} +static inline struct pi_desc *vcpu_to_pi_desc (struct kvm_vcpu *vcpu) { -return &(to_vmx (vcpu)->pi_desc); +return &vcpu_to_pi (vcpu)->pi_desc; }static int pi_try_set_control (struct pi_desc *pi_desc,u64 *pold,u64 new)
@@ -52,8 +73,8 @@ static int pi_try_set_control(struct pi_desc *pi_desc, u64 *pold, u64 new)void vmx_vcpu_pi_load (struct kvm_vcpu *vcpu,int cpu) { -struct pi_desc *pi_desc =vcpu_to_pi_desc (vcpu); -struct vcpu_vmx *vmx =to_vmx (vcpu); +struct vcpu_pi *vcpu_pi =vcpu_to_pi (vcpu); +struct pi_desc *pi_desc = &vcpu_pi->pi_desc;struct pi_desc old, new;unsigned long flags;unsigned int dest;
@@ -90,7 +111,7 @@ void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)*/ if (pi_desc->nv ==POSTED_INTR_WAKEUP_VECTOR ) {raw_spin_lock (&per_cpu (wakeup_vcpus_on_cpu_lock, vcpu->cpu)); -list_del (&vmx->pi_wakeup_list); +list_del (&vcpu_pi->pi_wakeup_list);raw_spin_unlock (&per_cpu (wakeup_vcpus_on_cpu_lock, vcpu->cpu)); }
@@ -145,15 +166,15 @@ static bool vmx_can_use_vtd_pi(struct kvm *kvm)*/ static void pi_enable_wakeup_handler (struct kvm_vcpu *vcpu) { -struct pi_desc *pi_desc =vcpu_to_pi_desc (vcpu); -struct vcpu_vmx *vmx =to_vmx (vcpu); +struct vcpu_pi *vcpu_pi =vcpu_to_pi (vcpu); +struct pi_desc *pi_desc = &vcpu_pi->pi_desc;struct pi_desc old, new;unsigned long flags;local_irq_save (flags);raw_spin_lock (&per_cpu (wakeup_vcpus_on_cpu_lock, vcpu->cpu)); -list_add_tail (&vmx->pi_wakeup_list, +list_add_tail (&vcpu_pi->pi_wakeup_list, &per_cpu (wakeup_vcpus_on_cpu, vcpu->cpu));raw_spin_unlock (&per_cpu (wakeup_vcpus_on_cpu_lock, vcpu->cpu));
@@ -190,7 +211,8 @@ static bool vmx_needs_pi_wakeup(struct kvm_vcpu *vcpu)* notification vector is switched to the one that calls * back to the pi_wakeup_handler() function. */ -return vmx_can_use_ipiv (vcpu) ||vmx_can_use_vtd_pi (vcpu->kvm); +return (vmx_can_use_ipiv (vcpu) && !is_td_vcpu (vcpu)) || +vmx_can_use_vtd_pi (vcpu->kvm); }void vmx_vcpu_pi_put (struct kvm_vcpu *vcpu)
@@ -200,7 +222,8 @@ void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)if (!vmx_needs_pi_wakeup (vcpu))return ; -if (kvm_vcpu_is_blocking (vcpu) && !vmx_interrupt_blocked (vcpu)) +if (kvm_vcpu_is_blocking (vcpu) && + (is_td_vcpu (vcpu) || !vmx_interrupt_blocked (vcpu)))pi_enable_wakeup_handler (vcpu);/*
arch/x86/kvm/vmx/posted_intr.h +11 -0
@@ -94,6 +94,17 @@ static inline bool pi_test_sn(struct pi_desc *pi_desc) (unsigned long *)&pi_desc->control); } +struct vcpu_pi { +struct kvm_vcpu vcpu; + +/* Posted interrupt descriptor */ +struct pi_desc pi_desc; + +/* Used if this vCPU is waiting for PI notification wakeup. */ +struct list_head pi_wakeup_list; +/* Until here common layout betwwn vcpu_vmx and vcpu_tdx. */ +}; +void vmx_vcpu_pi_load (struct kvm_vcpu *vcpu,int cpu);void vmx_vcpu_pi_put (struct kvm_vcpu *vcpu);void pi_wakeup_handler (void );
arch/x86/kvm/vmx/tdx.c +1 -0
@@ -558,6 +558,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)fpstate_set_confidential (&vcpu->arch.guest_fpu); vcpu->arch.apic->guest_apic_protected = true; +INIT_LIST_HEAD (&tdx->pi_wakeup_list); vcpu->arch.efer =EFER_SCE |EFER_LME |EFER_LMA |EFER_NX ;
arch/x86/kvm/vmx/tdx.h +8 -0
@@ -4,6 +4,7 @@#ifdef CONFIG_INTEL_TDX_HOST +#include "posted_intr.h" #include "pmu_intel.h" #include "tdx_ops.h"
@@ -67,6 +68,13 @@ union tdx_exit_reason {struct vcpu_tdx {struct kvm_vcpu vcpu; +/* Posted interrupt descriptor */ +struct pi_desc pi_desc; + +/* Used if this vCPU is waiting for PI notification wakeup. */ +struct list_head pi_wakeup_list; +/* Until here same layout to struct vcpu_pi. */ +unsigned long tdvpr_pa;unsigned long *tdvpx_pa;
arch/x86/kvm/vmx/vmx.h +8 -6
@@ -233,6 +233,14 @@ struct nested_vmx {struct vcpu_vmx {struct kvm_vcpu vcpu; + +/* Posted interrupt descriptor */ +struct pi_desc pi_desc; + +/* Used if this vCPU is waiting for PI notification wakeup. */ +struct list_head pi_wakeup_list; +/* Until here same layout to struct vcpu_pi. */ +u8 fail;u8 x2apic_msr_bitmap_mode;
@@ -302,12 +310,6 @@ struct vcpu_vmx {union vmx_exit_reason exit_reason; -/* Posted interrupt descriptor */ -struct pi_desc pi_desc; - -/* Used if this vCPU is waiting for PI notification wakeup. */ -struct list_head pi_wakeup_list; -/* Support for a guest hypervisor (nested VMX) */ struct nested_vmx nested;
Signature · 1 line
2.25.1
Quoted 1 line · On 7.11.23 г. 16:55 ч., [email protected] wrote:
- Switched to TDX module 1.5. Unsupport TDX module 1.0
Oh wow, so 1.0 got deprecated/b0rked even before TDX got an official release :)
Re: [PATCH v17 071/116] KVM: TDX: handle vcpu migration over logical processor
In reply to [email protected]Quoted 26 lines · On Tue, Nov 07, 2023 at 06:56:37AM -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
For vcpu migration, in the case of VMX, VMCS 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.
When shutting down the machine, (VMX or TDX) vcpus needs to be shutdown on each pcpu. Do the similar for TDX with TDX SEAMCALL APIs.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 32 ++++++- arch/x86/kvm/vmx/tdx.c | 190 ++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + arch/x86/kvm/vmx/x86_ops.h | 4 + 4 files changed, 221 insertions(+), 7 deletions(-)arch/x86/kvm/vmx/main.c +0
-0@@ -44,6 +44,14 @@ static int vt_hardware_enable(void)return ret; }
......
Quoted 33 lines
arch/x86/kvm/vmx/main.c +9
-4-void tdx_mmu_release_hkid (struct kvm *kvm) +static int __tdx_mmu_release_hkid (struct kvm *kvm) {bool packages_allocated, targets_allocated;struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm);cpumask_var_t packages, targets; +struct kvm_vcpu *vcpu; +unsigned long j; +int i, ret =0 ;u64 err; -int i;if (!is_hkid_assigned (kvm_tdx)) -return ; +return 0 ;if (!is_td_created (kvm_tdx)) {tdx_hkid_free (kvm_tdx); -return ; +return 0 ; } packages_allocated =zalloc_cpumask_var (&packages,GFP_KERNEL ); targets_allocated =zalloc_cpumask_var (&targets,GFP_KERNEL );cpus_read_lock (); +kvm_for_each_vcpu (j, vcpu, kvm) +tdx_flush_vp_on_cpu (vcpu); +/* * We can destroy multiple the guest TDs simultaneously. Prevent * tdh_phymem_cache_wb from returning TDX_BUSY by serialization.
arch/x86/kvm/vmx/main.c void tdx_mmu_release_hkid(struct kvm *kvm) · line 365
Quoted 5 lines
arch/x86/kvm/vmx/main.c +2
-0@@ -236,6 +361,19 @@ void tdx_mmu_release_hkid(struct kvm *kvm)*/ write_lock (&kvm->mmu_lock); + err =tdh_mng_vpflushdone (kvm_tdx->tdr_pa); +if (err ==TDX_FLUSHVP_NOT_DONE ) {
Not sure IIUC, The __tdx_mmu_release_hkid() is called in MMU release callback, which means all threads of the process have dropped mm by do_exit() so they won't run kvm code anymore, and tdx_flush_vp_on_cpu() is called for each pcpu they run last time, so will this error really happen ?
Quoted 147 lines
arch/x86/kvm/vmx/main.c +55
-0+ ret = -EBUSY ; + goto out; + } +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MNG_VPFLUSHDONE , err,NULL ); +pr_err ("tdh_mng_vpflushdone() failed. HKID %d is leaked.\n" , + kvm_tdx->hkid); + ret = -EIO ; + goto out; + } + for_each_online_cpu(i) { if (packages_allocated && cpumask_test_and_set_cpu(topology_physical_package_id(i),@@ -258,14 +396,24 @@ void tdx_mmu_release_hkid(struct kvm *kvm)pr_tdx_error (TDH_MNG_KEY_FREEID , err,NULL );pr_err ("tdh_mng_key_freeid() failed. HKID %d is leaked.\n" , kvm_tdx->hkid); + ret = -EIO ; }else tdx_hkid_free (kvm_tdx); +out:write_unlock (&kvm->mmu_lock);mutex_unlock (&tdx_lock);cpus_read_unlock ();free_cpumask_var (targets);free_cpumask_var (packages); + +return ret; +} + +void tdx_mmu_release_hkid (struct kvm *kvm) +{ +while (__tdx_mmu_release_hkid (kvm) == -EBUSY ) + ; }void tdx_vm_free (struct kvm *kvm)@@ -429,6 +577,26 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)return 0 ; } +void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (vcpu->cpu == cpu) +return ; + +tdx_flush_vp_on_cpu (vcpu); + +local_irq_disable (); +/* +* Pairs with the smp_wmb() in tdx_disassociate_vp() to ensure +* vcpu->cpu is read before tdx->cpu_list. +*/ +smp_rmb (); + +list_add (&tdx->cpu_list, &per_cpu (associated_tdvcpus, cpu)); +local_irq_enable (); +} +void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);@@ -469,6 +637,16 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu)struct vcpu_tdx *tdx =to_tdx (vcpu);int i; +/* +* When destroying VM, kvm_unload_vcpu_mmu() calls vcpu_load() for every +* vcpu after they already disassociated from the per cpu list by +* tdx_mmu_release_hkid(). So we need to disassociate them again, +* otherwise the freed vcpu data will be accessed when do +* list_{del,add}() on associated_tdvcpus list later. +*/ +tdx_disassociate_vp_on_cpu (vcpu); +WARN_ON_ONCE (vcpu->cpu !=-1 ); +/* * This methods can be called when vcpu allocation/initialization * failed. So it's possible that hkid, tdvpx and tdvpr are not assigned @@ -1873,6 +2051,10 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)return -EINVAL ; } +/* tdx_hardware_disable() uses associated_tdvcpus. */ +for_each_possible_cpu (i) +INIT_LIST_HEAD (&per_cpu (associated_tdvcpus, i)); +for (i =0 ; i <ARRAY_SIZE (tdx_uret_msrs); i++) {/* * Here it checks if MSRs (tdx_uret_msrs) can be saved/restored arch/x86/kvm/vmx/tdx.h +2
-0@@ -70,6 +70,8 @@ struct vcpu_tdx {unsigned long tdvpr_pa;unsigned long *tdvpx_pa; +struct list_head cpu_list; +union tdx_exit_reason exit_reason;bool initialized;arch/x86/kvm/vmx/x86_ops.h +4
-1@@ -137,6 +137,7 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);#ifdef CONFIG_INTEL_TDX_HOST int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);void tdx_hardware_unsetup (void ); +void tdx_hardware_disable (void );bool tdx_is_vm_type_supported (unsigned long type);int tdx_offline_cpu (void );@@ -153,6 +154,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);u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp);@@ -164,6 +166,7 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {} +static inline void tdx_hardware_disable (void ) {}static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }static inline int tdx_offline_cpu (void ) {return 0 ; }@@ -183,6 +186,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 u8 tdx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio) {return 0 ; }static inline int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp) {return -EOPNOTSUPP ; } --2.25.1
Re: [PATCH v17 015/116] x86/cpu: Add helper functions to allocate/free TDX private host key id
In reply to [email protected]Quoted 66 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Add helper functions to allocate/free TDX private host key id (HKID), and export the global TDX HKID.
The memory controller encrypts TDX memory with the assigned TDX HKIDs. The global TDX HKID is to encrypt the TDX module, its memory, and some dynamic data (TDR). The private TDX HKID is assigned to guest TD to encrypt guest memory and the related data. When VMM releases an encrypted page for reuse, the page needs a cache flush with the used HKID. VMM needs the global TDX HKID and the private TDX HKIDs to flush encrypted pages.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/tdx.h | 12 ++++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 28 +++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-)arch/x86/include/asm/tdx.h +12
-0@@ -114,6 +114,16 @@ int tdx_cpu_enable(void);int tdx_enable (void );void tdx_reset_memory (void );bool tdx_is_private_mem (unsigned long phys); + +/* +* Key id globally used by TDX module: TDX module maps TDR with this TDX global +* key id. TDR includes key id assigned to the TD. Then TDX module maps other +* TD-related pages with the assigned key id. TDR requires this TDX global key +* id for cache flush unlike other TD-related pages. +*/ +extern u32 tdx_global_keyid; +int tdx_guest_keyid_alloc (void ); +void tdx_guest_keyid_free (int keyid);#else static inline u64 __seamcall (u64 fn,struct tdx_module_args *args) {@@ -132,6 +142,8 @@ static inline int tdx_cpu_enable(void) { return -ENODEV; }static inline int tdx_enable (void ) {return -ENODEV ; }static inline void tdx_reset_memory (void ) { }static inline bool tdx_is_private_mem (unsigned long phys) {return false; } +static inline int tdx_guest_keyid_alloc (void ) {return -EOPNOTSUPP ; } +static inline void tdx_guest_keyid_free (int keyid) { }#endif /* CONFIG_INTEL_TDX_HOST */ #endif /* !__ASSEMBLY__ */ arch/x86/virt/vmx/tdx/tdx.c +2
-1@@ -37,7 +37,8 @@#include <asm/tdx.h> #include "tdx.h" -static u32 tdx_global_keyid__ro_after_init ; +u32 tdx_global_keyid__ro_after_init ; +EXPORT_SYMBOL_GPL (tdx_global_keyid);static u32 tdx_guest_keyid_start__ro_after_init ;static u32 tdx_nr_guest_keyids__ro_after_init ;@@ -105,6 +106,31 @@ static inline int sc_retry_prerr(sc_func_t func, sc_err_func_t err_func,#define seamcall_prerr_ret (__fn, __args) \ sc_retry_prerr(__seamcall_ret, seamcall_err_ret, (__fn), (__args))
arch/x86/virt/vmx/tdx/tdx.c static inline int sc_retry_prerr(sc_func_t func, sc_err_func_t err_func, · line 118
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10
-0+/* TDX KeyID pool */ +static DEFINE_IDA (tdx_guest_keyid_pool); + +int tdx_guest_keyid_alloc (void ) +{ +if (WARN_ON_ONCE (!tdx_guest_keyid_start || !tdx_nr_guest_keyids)) +return -EINVAL ; + +/* The first keyID is reserved for the global key. */ + return ida_alloc_range(&tdx_guest_keyid_pool, tdx_guest_keyid_start + 1,
Per
https://lore.kernel.org/all/121aab11b48b4e6550cfe6d23b4daab744ee2076.1697532085.git.kai.huang@intel.com/tdx_guest_keyid_start has already reserved the first keyID for global key, I think we don't need to reserve another one here.
Quoted 18 lines
arch/x86/virt/vmx/tdx/tdx.c +15
-0+ tdx_guest_keyid_start + tdx_nr_guest_keyids -1 , + GFP_KERNEL); +} +EXPORT_SYMBOL_GPL (tdx_guest_keyid_alloc); + +void tdx_guest_keyid_free (int keyid) +{ +/* keyid = 0 is reserved. */ +if (WARN_ON_ONCE (keyid <=0 )) +return ; + +ida_free (&tdx_guest_keyid_pool, keyid); +} +EXPORT_SYMBOL_GPL (tdx_guest_keyid_free); +/* * Do the module global initialization once and return its result. * It can be done on any cpu. It's always called with interrupts
Re: [PATCH v17 059/116] KVM: TDX: Create initial guest memory
In reply to [email protected]Quoted 34 lines · On 11/7/2023 10:56 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Because the guest memory is protected in TDX, the creation of the initial guest memory requires a dedicated TDX module API,
tdh_mem_page_add, instead of directly copying the memory contents into the guest memory in the case of the default VM type. KVM MMU page fault handler callback,private_page_add, handles it.Define new subcommand,
KVM_TDX_INIT_MEM_REGION, of VM-scopedKVM_MEMORY_ENCRYPT_OP. It assigns the guest page, copies the initial memory contents into the guest memory, encrypts the guest memory. At the same time, optionally it extends memory measurement of the TDX guest. It calls the KVM MMU page fault(EPT-violation) handler to trigger the callbacks for it.Reported-by [email protected]Signed-off-by Isaku Yamahata <[email protected]>---
v15 -> v16:
- add check ifnr_pagesisn't large with
(nr_page<<PAGE_SHIFT) >>PAGE_SHIFTv14 -> v15:
- add a check if TD is finalized or not totdx_init_mem_region()
- return-EAGAINwhen partial population--- arch/x86/include/uapi/asm/kvm.h | 9 ++ arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/tdx.c | 167 +++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + tools/arch/x86/include/uapi/asm/kvm.h | 9 ++ 5 files changed, 185 insertions(+), 3 deletions(-)
[...]
arch/x86/kvm/vmx/tdx.c static int tdx_sept_page_aug(struct kvm *kvm, gfn_t gfn, · line 534
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +2
-0+static int tdx_sept_page_add (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn)
For me, the function name is a bit confusing.
I would relate it to a SEPT table page instead of a normal private page if only by the function name.
Similar to tdx_sept_page_aug(), though it's less confusing due to there is no seam call to aug a sept table page.
Quoted 53 lines
arch/x86/kvm/vmx/tdx.c +53
-0+{ +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_args out; +hpa_t source_pa; +bool measure; +u64 err; + +/* +* KVM_INIT_MEM_REGION, tdx_init_mem_region(), supports only 4K page +* because tdh_mem_page_add() supports only 4K page. +*/ +if (KVM_BUG_ON (level !=PG_LEVEL_4K , kvm)) +return -EINVAL ; + +/* +* 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). +*/ +if (KVM_BUG_ON (kvm_tdx->source_pa ==INVALID_PAGE , kvm)) { +tdx_unpin (kvm, pfn); +return -EINVAL ; + } + + source_pa = kvm_tdx->source_pa & ~KVM_TDX_MEASURE_MEMORY_REGION ; + measure = kvm_tdx->source_pa &KVM_TDX_MEASURE_MEMORY_REGION ; + kvm_tdx->source_pa =INVALID_PAGE ; + +do { + err =tdh_mem_page_add (kvm_tdx->tdr_pa, gpa, hpa, source_pa, + &out); +/* +* This path is executed during populating initial guest memory +* image. i.e. before running any vcpu. Race is rare. +*/ + }while (unlikely (err ==TDX_ERROR_SEPT_BUSY )); +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_PAGE_ADD , err, &out); +tdx_unpin (kvm, pfn); +return -EIO ; + }else if (measure) +tdx_measure_page (kvm_tdx, gpa); + +return 0 ; + +} +
[...]
Re: [PATCH v17 015/116] x86/cpu: Add helper functions to allocate/free TDX private host key id
In reply to Chenyi QiangQuoted 18 lines · Chenyi Qiang
Quoted 18 lines
arch/x86/virt/vmx/tdx/tdx.c +2
-1@@ -37,7 +37,8 @@#include <asm/tdx.h> #include "tdx.h" -static u32 tdx_global_keyid__ro_after_init ; +u32 tdx_global_keyid__ro_after_init ; +EXPORT_SYMBOL_GPL (tdx_global_keyid);static u32 tdx_guest_keyid_start__ro_after_init ;static u32 tdx_nr_guest_keyids__ro_after_init ;@@ -105,6 +106,31 @@ static inline int sc_retry_prerr(sc_func_t func, sc_err_func_t err_func,#define seamcall_prerr_ret (__fn, __args) \ sc_retry_prerr(__seamcall_ret, seamcall_err_ret, (__fn), (__args))
Quoted 15 lines
arch/x86/virt/vmx/tdx/tdx.c static inline int sc_retry_prerr(sc_func_t func, sc_err_func_t err_func, · line 118
Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10
-0+/* TDX KeyID pool */ +static DEFINE_IDA (tdx_guest_keyid_pool); + +int tdx_guest_keyid_alloc (void ) +{ +if (WARN_ON_ONCE (!tdx_guest_keyid_start || !tdx_nr_guest_keyids)) +return -EINVAL ; + +/* The first keyID is reserved for the global key. */ + return ida_alloc_range(&tdx_guest_keyid_pool, tdx_guest_keyid_start + 1,Per
https://lore.kernel.org/all/121aab11b48b4e6550cfe6d23b4daab744ee2076.1697532085.git.kai.huang@intel.com/tdx_guest_keyid_starthas already reserved the first keyID for global key, I think we don't need to reserve another one here.
Nice catch. Will fix it with the next respin.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v17 059/116] KVM: TDX: Create initial guest memory
In reply to Binbin WuQuoted 45 lines · Binbin Wu
Quoted 34 lines · On 11/7/2023 10:56 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Because the guest memory is protected in TDX, the creation of the initial guest memory requires a dedicated TDX module API,
tdh_mem_page_add, instead of directly copying the memory contents into the guest memory in the case of the default VM type. KVM MMU page fault handler callback,private_page_add, handles it.Define new subcommand,
KVM_TDX_INIT_MEM_REGION, of VM-scopedKVM_MEMORY_ENCRYPT_OP. It assigns the guest page, copies the initial memory contents into the guest memory, encrypts the guest memory. At the same time, optionally it extends memory measurement of the TDX guest. It calls the KVM MMU page fault(EPT-violation) handler to trigger the callbacks for it.Reported-by [email protected]Signed-off-by Isaku Yamahata <[email protected]>---
v15 -> v16:
- add check ifnr_pagesisn't large with
(nr_page<<PAGE_SHIFT) >>PAGE_SHIFTv14 -> v15:
- add a check if TD is finalized or not totdx_init_mem_region()
- return-EAGAINwhen partial population--- arch/x86/include/uapi/asm/kvm.h | 9 ++ arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/tdx.c | 167 +++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + tools/arch/x86/include/uapi/asm/kvm.h | 9 ++ 5 files changed, 185 insertions(+), 3 deletions(-)[...]
arch/x86/kvm/vmx/tdx.c static int tdx_sept_page_aug(struct kvm *kvm, gfn_t gfn, · line 534
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+static int tdx_sept_page_add (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn)For me, the function name is a bit confusing.
I would relate it to a SEPT table page instead of a normal private page if only by the function name.
Quoted 2 lines
Similar to
tdx_sept_page_aug(), though it's less confusing due to there is no seam call to aug a sept table page.
How about tdx_mem_page_{add, aug}()?
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v17 059/116] KVM: TDX: Create initial guest memory
Quoted 46 lines · Isaku Yamahata
Quoted 43 lines · Binbin Wu
Quoted 34 lines · On 11/7/2023 10:56 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Because the guest memory is protected in TDX, the creation of the initial guest memory requires a dedicated TDX module API,
tdh_mem_page_add, instead of directly copying the memory contents into the guest memory in the case of the default VM type. KVM MMU page fault handler callback,private_page_add, handles it.Define new subcommand,
KVM_TDX_INIT_MEM_REGION, of VM-scopedKVM_MEMORY_ENCRYPT_OP. It assigns the guest page, copies the initial memory contents into the guest memory, encrypts the guest memory. At the same time, optionally it extends memory measurement of the TDX guest. It calls the KVM MMU page fault(EPT-violation) handler to trigger the callbacks for it.Reported-by [email protected]Signed-off-by Isaku Yamahata <[email protected]>---
v15 -> v16:
- add check ifnr_pagesisn't large with
(nr_page<<PAGE_SHIFT) >>PAGE_SHIFTv14 -> v15:
- add a check if TD is finalized or not totdx_init_mem_region()
- return-EAGAINwhen partial population--- arch/x86/include/uapi/asm/kvm.h | 9 ++ arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/vmx/tdx.c | 167 +++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + tools/arch/x86/include/uapi/asm/kvm.h | 9 ++ 5 files changed, 185 insertions(+), 3 deletions(-)[...]
arch/x86/kvm/vmx/tdx.c static int tdx_sept_page_aug(struct kvm *kvm, gfn_t gfn, · line 534
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+static int tdx_sept_page_add (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn)For me, the function name is a bit confusing.
I would relate it to a SEPT table page instead of a normal private page if only by the function name.
Quoted 3 lines
Quoted 2 lines
Similar to
tdx_sept_page_aug(), though it's less confusing due to there is no seam call to aug a sept table page.How about tdx_mem_page_{add, aug}()?
It looks good to me.
Re: [PATCH v17 071/116] KVM: TDX: handle vcpu migration over logical processor
In reply to Yuan YaoQuoted 64 lines · Yuan Yao
Quoted 26 lines · On Tue, Nov 07, 2023 at 06:56:37AM -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
For vcpu migration, in the case of VMX, VMCS 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.
When shutting down the machine, (VMX or TDX) vcpus needs to be shutdown on each pcpu. Do the similar for TDX with TDX SEAMCALL APIs.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 32 ++++++- arch/x86/kvm/vmx/tdx.c | 190 ++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 2 + arch/x86/kvm/vmx/x86_ops.h | 4 + 4 files changed, 221 insertions(+), 7 deletions(-)arch/x86/kvm/vmx/main.c +0
-0@@ -44,6 +44,14 @@ static int vt_hardware_enable(void)return ret; }......
Quoted 36 lines
arch/x86/kvm/vmx/main.c +9
-4-void tdx_mmu_release_hkid (struct kvm *kvm) +static int __tdx_mmu_release_hkid (struct kvm *kvm) {bool packages_allocated, targets_allocated;struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm);cpumask_var_t packages, targets; +struct kvm_vcpu *vcpu; +unsigned long j; +int i, ret =0 ;u64 err; -int i;if (!is_hkid_assigned (kvm_tdx)) -return ; +return 0 ;if (!is_td_created (kvm_tdx)) {tdx_hkid_free (kvm_tdx); -return ; +return 0 ; } packages_allocated =zalloc_cpumask_var (&packages,GFP_KERNEL ); targets_allocated =zalloc_cpumask_var (&targets,GFP_KERNEL );cpus_read_lock (); +kvm_for_each_vcpu (j, vcpu, kvm) +tdx_flush_vp_on_cpu (vcpu); +/* * We can destroy multiple the guest TDs simultaneously. Prevent * tdh_phymem_cache_wb from returning TDX_BUSY by serialization. @@ -236,6 +361,19 @@ void tdx_mmu_release_hkid(struct kvm *kvm)*/ write_lock (&kvm->mmu_lock);
Quoted 8 lines
arch/x86/kvm/vmx/main.c void tdx_mmu_release_hkid(struct kvm *kvm) · line 365
Quoted 2 lines
arch/x86/kvm/vmx/main.c +2
-0+ err =tdh_mng_vpflushdone (kvm_tdx->tdr_pa); +if (err ==TDX_FLUSHVP_NOT_DONE ) {Not sure IIUC, The
__tdx_mmu_release_hkid()is called in MMU release callback, which means all threads of the process have dropped mm bydo_exit()so they won't run kvm code anymore, andtdx_flush_vp_on_cpu()is called for each pcpu they run last time, so will this error really happen ?
KVM_TDX_RELEASE_VM calls the function too. Maybe this check should be introduced with the patch for KVM_TDX_RELEASE_VM.
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v17 020/116] KVM: TDX: create/destroy VM structure
In reply to [email protected]Quoted 22 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
As the first step to create TDX guest, create/destroy VM struct. Assign TDX private Host Key ID (HKID) to the TDX guest for memory encryption and allocate extra pages for the TDX guest. On destruction, free allocated pages, and HKID.
Before tearing down private page tables, TDX requires some resources of the guest TD to be destroyed (i.e. HKID must have been reclaimed, etc). Add mmu notifier release callback before tearing down private page tables for it.
Add
vm_free()ofkvm_x86_opshook at the end ofkvm_arch_destroy_vm()because some per-VM TDX resources, e.g. TDR, need to be freed after other TDX resources, e.g. HKID, were freed.Co-developed-by Kai Huang <[email protected]>Signed-off-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
[...]
arch/x86/kvm/vmx/tdx.c int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 145
Quoted 12 lines
arch/x86/kvm/vmx/tdx.c +12
-0+ +static int tdx_reclaim_page (hpa_t pa) +{ +int r; + + r =__tdx_reclaim_page (pa); +if (!r) +tdx_clear_page (pa); +return r; +} + +static void tdx_reclaim_td_page (unsigned long td_page_pa)
This function is used to reclaim td control sturcture pages like TDCX, TDVPX, TDVPR. Should this function name be more specific?
For me, it is a bit confusing.
Or maybe do "td page" have specific meaning referring to these control structures pages in TDX?
Quoted 23 lines
arch/x86/kvm/vmx/tdx.c +23
-0+{ +WARN_ON_ONCE (!td_page_pa); + +/* +* TDCX are being reclaimed. TDX module maps TDCX with HKID +* assigned to the TD. Here the cache associated to the TD +* was already flushed by TDH.PHYMEM.CACHE.WB before here, So +* cache doesn't need to be flushed again. +*/ +if (tdx_reclaim_page (td_page_pa)) +/* +* Leak the page on failure: +* tdx_reclaim_page() returns an error if and only if there's an +* unexpected, fatal error, e.g. a SEAMCALL with bad params, +* incorrect concurrency in KVM, a TDX Module bug, etc. +* Retrying at a later point is highly unlikely to be +* successful. +* No log here as tdx_reclaim_page() already did. +*/ +return ; +free_page ((unsigned long )__va (td_page_pa)); +} +
[...]
Re: [PATCH v17 020/116] KVM: TDX: create/destroy VM structure
Quoted 17 lines · Binbin Wu
arch/x86/kvm/vmx/tdx.c int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 145
Quoted 11 lines
arch/x86/kvm/vmx/tdx.c +11
-0+static int tdx_reclaim_page (hpa_t pa) +{ +int r; + + r =__tdx_reclaim_page (pa); +if (!r) +tdx_clear_page (pa); +return r; +} + +static void tdx_reclaim_td_page (unsigned long td_page_pa)This function is used to reclaim td control sturcture pages like TDCX, TDVPX,
TDVPR. Should this function name be more specific?
For me, it is a bit confusing.
Quoted 3 lines
Or maybe do "td page" have specific meaning referring to these control structures
pages in TDX?
As they are control page, how about tdx_reclaim_control_page()?
Signature · 1 line
Isaku Yamahata <[email protected]>
Re: [PATCH v17 004/116] KVM: VMX: Reorder vmx initialization with kvm vendor initialization
In reply to [email protected]Quoted 14 lines · On Tue, Nov 07, 2023 at 06:55:30AM -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To match
vmx_exitcleanup.Signed-off-by Isaku Yamahata <[email protected]>---
arch/x86/kvm/vmx/main.c| 10 +++++-----1 file changed, 5 insertions(+), 5 deletions(-)arch/x86/kvm/vmx/main.c +0
-0
arch/x86/kvm/vmx/main.c static int __init vt_init(void) · line 186
Quoted 9 lines
arch/x86/kvm/vmx/main.c +2
-2@@ -180,11 +180,11 @@ static int __init vt_init(void)*/ hv_init_evmcs (); - r =kvm_x86_vendor_init (&vt_init_ops); + r =vmx_init ();if (r) -return r; + goto err_vmx_init;
this is incorrect. vmx_exit() shouldn't be called if vmx_init() failed.
Quoted 22 lines
arch/x86/kvm/vmx/main.c +1
-1- r =vmx_init (); + r =kvm_x86_vendor_init (&vt_init_ops);if (r) goto err_vmx_init;@@ -201,9 +201,9 @@ static int __init vt_init(void)return 0 ;
err_kvm_init:arch/x86/kvm/vmx/main.c +0
-2-vmx_exit (); -err_vmx_init:kvm_x86_vendor_exit();arch/x86/kvm/vmx/main.c +2
-0+err_vmx_init: +vmx_exit ();return r; }
module_init(vt_init);Signature · 1 line
2.25.1
Re: [PATCH v17 001/116] KVM: VMX: Move out vmx_x86_ops to 'main.c' to wrap VMX and TDX
In reply to [email protected]Quoted 40 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Sean Christopherson <[email protected]>
KVM accesses Virtual Machine Control Structure (VMCS) with VMX instructions to operate on VM. TDX doesn't allow VMM to operate VMCS directly. Instead, TDX has its own data structures, and TDX SEAMCALL APIs for VMM to indirectly operate those data structures. This means we must have a TDX version of
kvm_x86_ops.The existing global struct
kvm_x86_opsalready defines an interface which fits with TDX. Butkvm_x86_opsis system-wide, not per-VM structure. To allow VMX to coexist with TDs, thekvm_x86_opscallbacks will have wrappers "if (tdx)tdx_op()elsevmx_op()" to switch VMX or TDX at run time.To split the runtime switch, the VMX implementation, and the TDX implementation, add
main.c, and move out thevmx_x86_opshooks in preparation for adding TDX, which can coexist with VMX, i.e. KVM can run both VMs and TDs. Use 'vt' for the naming scheme as a nod to VT-x and as a concatenation of VmxTdx.The current code looks as follows.
Invmx.c
staticvmx_op(){ ... }static struct kvm_x86_ops vmx_x86_ops = { .op = vmx_op,initialization code
The eventually converted code will look like
Invmx.c, keep the VMX operations.
vmx_op(){ ... }
VMX initialization
Intdx.c, define the TDX operations.
tdx_op(){ ... }
TDX initialization
Inx86_ops.h, declare the VMX and TDX operations.vmx_op(); tdx_op(); In main.c, define common wrappers for VMX and TDX. static vt_ops() { if (tdx) tdx_ops() else vmx_ops() } static struct kvm_x86_ops vt_x86_ops = { .op = vt_op,
Quoted 4 lines
initialization to call VMX and TDX initialization
Opportunistically, fix the name inconsistency from
vmx_create_vcpu()andvmx_free_vcpu()tovmx_vcpu_create()andvxm_vcpu_free().
Nit: 3 indentation alignment below.
Quoted 29 lines
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]>Reviewed-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/vmx/main.c | 167 +++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 374 ++++++++++--------------------------- arch/x86/kvm/vmx/x86_ops.h | 125 +++++++++++++ 4 files changed, 395 insertions(+), 273 deletions(-)create mode 100644
arch/x86/kvm/vmx/main.c
create mode 100644arch/x86/kvm/vmx/x86_ops.harch/x86/kvm/Makefile +1
-1@@ -23,7 +23,7 @@ kvm-$(CONFIG_KVM_XEN) += xen.o kvm-$(CONFIG_KVM_SMM) += smm.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ - vmx/hyperv.o vmx/nested.o vmx/posted_intr.o + vmx/hyperv.o vmx/nested.o vmx/posted_intr.o vmx/main.o kvm-intel-$(CONFIG_X86_SGX_KVM) += vmx/sgx.o kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o \arch/x86/kvm/vmx/main.c +0
-0
[...]
arch/x86/kvm/vmx/main.c
Quoted 4 lines
arch/x86/kvm/vmx/main.c +1
-1-static int vmx_check_emulate_instruction (struct kvm_vcpu *vcpu,int emul_type, +int vmx_check_emulate_instruction (struct kvm_vcpu *vcpu,int emul_type,void *insn,int insn_len)
Indentaion alignment
Quoted 15 lines
{ /*arch/x86/kvm/vmx/main.c +1
-1@@ -1742,7 +1740,7 @@ static int skip_emulated_instruction(struct kvm_vcpu *vcpu)* Recognizes a pending MTF VM-exit and records the nested state for later * delivery. */ -static void vmx_update_emulated_instruction (struct kvm_vcpu *vcpu) +void vmx_update_emulated_instruction (struct kvm_vcpu *vcpu) {struct vmcs12 *vmcs12 =get_vmcs12 (vcpu);struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -1773,7 +1771,7 @@ static void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu) } }
[...]
arch/x86/kvm/vmx/main.c
Quoted 3 lines
arch/x86/kvm/vmx/main.c +3
-0+u8 vmx_get_mt_mask (struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio); +void vmx_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason, +u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code);
Indentation alignment
arch/x86/kvm/vmx/main.c
Quoted 10 lines
arch/x86/kvm/vmx/main.c +10
-0+u64 vmx_get_l2_tsc_offset (struct kvm_vcpu *vcpu); +u64 vmx_get_l2_tsc_multiplier (struct kvm_vcpu *vcpu); +void vmx_write_tsc_offset (struct kvm_vcpu *vcpu); +void vmx_write_tsc_multiplier (struct kvm_vcpu *vcpu); +void vmx_request_immediate_exit (struct kvm_vcpu *vcpu); +void vmx_sched_in (struct kvm_vcpu *vcpu,int cpu); +void vmx_update_cpu_dirty_logging (struct kvm_vcpu *vcpu); +#ifdef CONFIG_X86_64 +int vmx_set_hv_timer (struct kvm_vcpu *vcpu,u64 guest_deadline_tsc, +bool *expired);
Indentation alignment
Quoted 5 lines
arch/x86/kvm/vmx/main.c +5
-0+void vmx_cancel_hv_timer (struct kvm_vcpu *vcpu); +#endif +void vmx_setup_mce (struct kvm_vcpu *vcpu); + +#endif /* __KVM_X86_VMX_X86_OPS_H */
Re: [PATCH v17 002/116] KVM: x86/vmx: initialize loaded_vmcss_on_cpu in vmx_hardware_setup()
In reply to [email protected]Quoted 6 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
vmx_hardware_disable()accessesloaded_vmcss_on_cpuviahardware_disable_all(). To allow hardware_enable/disable_all() beforekvm_init(), initialize it invmx_hardware_setup()so that tdx module initialization,hardware_setupmethod, can reference the variable.
In patch 004/116, vmx_init() and kvm_x86_vendor_init() are re-ordered, tdx_hardware_setup() will be called after vmx_init(), the change of this patch seems meaningless after patch 004/116.
Quoted 36 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)arch/x86/kvm/vmx/vmx.c +5
-4@@ -8263,8 +8263,12 @@ __init int vmx_hardware_setup(void) {unsigned long host_bndcfgs;struct desc_ptr dt; +int cpu;int r; +/* vmx_hardware_disable() accesses loaded_vmcss_on_cpu. */ +for_each_possible_cpu (cpu) +INIT_LIST_HEAD (&per_cpu (loaded_vmcss_on_cpu, cpu));store_idt (&dt); host_idt_base = dt.address;@@ -8507,11 +8511,8 @@ static int __init vmx_init(void)if (r) goto err_l1d_flush; -for_each_possible_cpu (cpu) { -INIT_LIST_HEAD (&per_cpu (loaded_vmcss_on_cpu, cpu)); - +for_each_possible_cpu (cpu)pi_init_cpu (cpu); - }cpu_emergency_register_virt_callback (vmx_emergency_disable);
Re: [PATCH v17 003/116] KVM: x86/vmx: Refactor KVM VMX module init/exit functions
In reply to [email protected]Quoted 38 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Currently, KVM VMX module initialization/exit functions are a single function each. Refactor KVM VMX module initialization functions into KVM common part and VMX part so that TDX specific part can be added cleanly. Opportunistically refactor module exit function as well.
The current module initialization flow is,
0.) Check if VMX is supported,
1.) hyper-v specific initialization,
2.) system-wide x86 specific and vendor specific initialization, 3.) Final VMX specific system-wide initialization,
4.) calculate the sizes of VMX kvm structure and VMX vcpu structure, 5.) report those sizes to the KVM common layer and KVM common
initializationRefactor the KVM VMX module initialization function into functions with a wrapper function to separate VMX logic in
vmx.cfrom a file,main.c, common among VMX and TDX. Introduce a wrapper function forvmx_init().The KVM architecture common layer allocates struct kvm with reported size for architecture-specific code. The KVM VMX module defines its structure as struct
vmx_kvm{ struct kvm; VMX specific members;} and uses it as struct vmx kvm. Similar for vcpu structure. TDX KVM patches will define TDX specific kvm and vcpu structures.The current module exit function is also a single function, a combination of VMX specific logic and common KVM logic. Refactor it into VMX specific logic and KVM common logic. This is just refactoring to keep the VMX specific logic in
vmx.cfrommain.c.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 50 +++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 54 +++++--------------------------------- arch/x86/kvm/vmx/x86_ops.h | 13 ++++++++- 3 files changed, 68 insertions(+), 49 deletions(-)
[...]
Quoted 5 lines
arch/x86/kvm/vmx/x86_ops.h +0
-0
arch/x86/kvm/vmx/x86_ops.h line 13
Quoted 9 lines
arch/x86/kvm/vmx/x86_ops.h +5
-1@@ -6,11 +6,22 @@#include "x86.h" -__init int vmx_hardware_setup (void ); +#if IS_ENABLED (CONFIG_HYPERV ) +__init void hv_init_evmcs (void ); +void hv_reset_evmcs (void ); +#else /* IS_ENABLED(CONFIG_HYPERV) */ +static inline void hv_init_evmcs (void ) {}
Does this one need "__init"?
Though it's empty, I checked exist kernel code, other empty static inline ones add "__init" for init code.
Quoted 14 lines
arch/x86/kvm/vmx/x86_ops.h +7
-0+static inline void hv_reset_evmcs (void ) {} +#endif /* IS_ENABLED(CONFIG_HYPERV) */ + +bool kvm_is_vmx_supported (void ); +int __init vmx_init (void ); +void vmx_exit (void );extern struct kvm_x86_ops vt_x86_ops__initdata ;extern struct kvm_x86_init_ops vt_init_ops__initdata ; +__init int vmx_hardware_setup (void );void vmx_hardware_unsetup (void );int vmx_check_processor_compat (void );int vmx_hardware_enable (void );
Re: [PATCH v17 004/116] KVM: VMX: Reorder vmx initialization with kvm vendor initialization
In reply to Chao GaoQuoted 26 lines · Chao Gao
Quoted 14 lines · On Tue, Nov 07, 2023 at 06:55:30AM -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
To match
vmx_exitcleanup.Signed-off-by Isaku Yamahata <[email protected]>---
arch/x86/kvm/vmx/main.c| 10 +++++-----1 file changed, 5 insertions(+), 5 deletions(-)arch/x86/kvm/vmx/main.c +0
-0arch/x86/kvm/vmx/main.c static int __init vt_init(void) · line 185
Quoted 8 lines
arch/x86/kvm/vmx/main.c +2
-2@@ -180,11 +180,11 @@ static int __init vt_init(void)*/ hv_init_evmcs (); - r =kvm_x86_vendor_init (&vt_init_ops); + r =vmx_init ();if (r) -return r; + goto err_vmx_init;this is incorrect.
vmx_exit()shouldn't be called ifvmx_init()failed.
Quoted 4 lines
arch/x86/kvm/vmx/main.c static int __init vt_init(void) · line 189
Quoted 4 lines
arch/x86/kvm/vmx/main.c +1
-1- r =vmx_init (); + r =kvm_x86_vendor_init (&vt_init_ops);if (r) goto err_vmx_init;
And also, maybe better to rename the lable, e.g, err_vendor_init?
Quoted 17 lines
Quoted 17 lines
arch/x86/kvm/vmx/main.c +0
-0@@ -201,9 +201,9 @@ static int __init vt_init(void)return 0 ;
err_kvm_init:arch/x86/kvm/vmx/main.c +0
-2-vmx_exit (); -err_vmx_init:kvm_x86_vendor_exit();arch/x86/kvm/vmx/main.c +2
-0+err_vmx_init: +vmx_exit ();return r; }
module_init(vt_init);Signature · 1 line
2.25.1
Re: [PATCH v17 005/116] KVM: TDX: Initialize the TDX module when loading the KVM intel kernel module
In reply to [email protected]Quoted 58 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX requires several initialization steps for KVM to create guest TDs. Detect CPU feature, enable VMX (TDX is based on VMX) on all online CPUs, detect the TDX module availability, initialize it and disable VMX.
To enable/disable VMX on all online CPUs, utilize vmx_hardware_enable/disable(). The method also initializes each CPU for TDX. TDX requires calling a TDX initialization function per logical processor (LP) before the LP uses TDX. When the CPU is becoming online, call the TDX LP initialization API. If it fails to initialize TDX, refuse CPU online for simplicity instead of TDX avoiding the failed LP.
There are several options on when to initialize the TDX module. A.) kernel module loading time, B.) the first guest TD creation time. A.) was chosen. With B.), a user may hit an error of the TDX initialization when trying to create the first guest TD. The machine that fails to initialize the TDX module can't boot any guest TD further. Such failure is undesirable and a surprise because the user expects that the machine can accommodate guest TD, but not. So A.) is better than B.).
Introduce a module parameter, kvm_intel.tdx, to explicitly enable TDX KVM support. It's off by default to keep the same behavior for those who don't use TDX. Implement
hardware_setupmethod to detect TDX feature of CPU and initialize TDX module.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/Makefile | 1 + arch/x86/kvm/vmx/main.c | 34 ++++++++++++++- arch/x86/kvm/vmx/tdx.c | 84 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 8 ++++ 4 files changed, 125 insertions(+), 2 deletions(-)create mode 100644
arch/x86/kvm/vmx/tdx.carch/x86/kvm/Makefile +1
-0@@ -25,6 +25,7 @@ kvm-$(CONFIG_KVM_SMM) += smm.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ vmx/hyperv.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 kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o \ svm/sev.o svm/hyperv.oarch/x86/kvm/vmx/main.c +2
-0@@ -6,6 +6,36 @@#include "nested.h" #include "pmu.h" +static bool enable_tdx__ro_after_init ; +module_param_named (tdx, enable_tdx, bool,0444 );
arch/x86/kvm/vmx/main.c line 20
Quoted 10 lines
arch/x86/kvm/vmx/main.c +10
-0+ +static int vt_hardware_enable (void ) +{ +int ret; + + ret =vmx_hardware_enable (); +if (ret || !enable_tdx) +return ret; + + ret =tdx_cpu_enable ();
Maybe it's worth a comment to describe this function can only fail on CPU online path. So if it fails, the CPU will not online.
IMHO, without any comment, it's not so easy to understand the call of vmx_hardware_disable() on failure.
Quoted 153 lines
arch/x86/kvm/vmx/main.c +20
-2+if (ret) +vmx_hardware_disable (); +return ret; +} + +static __init int vt_hardware_setup (void ) +{ +int ret; + + ret =vmx_hardware_setup (); +if (ret) +return ret; + + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops); + +return 0 ; +} +#define VMX_REQUIRED_APICV_INHIBITS \ (BIT(APICV_INHIBIT_REASON_DISABLE)| \ BIT(APICV_INHIBIT_REASON_ABSENT) | \@@ -22,7 +52,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .hardware_unsetup = vmx_hardware_unsetup, - .hardware_enable = vmx_hardware_enable, + .hardware_enable = vt_hardware_enable, .hardware_disable = vmx_hardware_disable, .has_emulated_msr = vmx_has_emulated_msr,@@ -159,7 +189,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { };struct kvm_x86_init_ops vt_init_ops__initdata = { - .hardware_setup = vmx_hardware_setup, + .hardware_setup = vt_hardware_setup, .handle_intel_pt_intr =NULL , .runtime_ops = &vt_x86_ops,arch/x86/kvm/vmx/tdx.c new file +84
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/cpu.h> + +#include <asm/tdx.h> + +#include "capabilities.h" +#include "x86_ops.h" +#include "x86.h" + +#undef pr_fmt +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +static int __init tdx_module_setup (void ) +{ +int ret; + + ret =tdx_enable (); +if (ret) { +pr_info ("Failed to initialize TDX module.\n" ); +return ret; + } + +return 0 ; +} + +struct vmx_tdx_enabled { +cpumask_var_t vmx_enabled; +atomic_t err; +}; + +static void __init vmx_tdx_on (void *_vmx_tdx) +{ +struct vmx_tdx_enabled *vmx_tdx = _vmx_tdx; +int r; + + r =vmx_hardware_enable (); +if (!r) { +cpumask_set_cpu (smp_processor_id (), vmx_tdx->vmx_enabled); + r =tdx_cpu_enable (); + } +if (r) +atomic_set (&vmx_tdx->err, r); +} + +static void __init vmx_off (void *_vmx_enabled) +{ +cpumask_var_t *vmx_enabled = (cpumask_var_t *)_vmx_enabled; + +if (cpumask_test_cpu (smp_processor_id (), *vmx_enabled)) +vmx_hardware_disable (); +} + +int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops) +{ +struct vmx_tdx_enabled vmx_tdx = { + .err =ATOMIC_INIT (0 ), + }; +int r =0 ; + +if (!enable_ept) { +pr_warn ("Cannot enable TDX with EPT disabled\n" ); +return -EINVAL ; + } + +if (!zalloc_cpumask_var (&vmx_tdx.vmx_enabled,GFP_KERNEL )) { + r = -ENOMEM ; + goto out; + } + +/* tdx_enable() in tdx_module_setup() requires cpus lock. */ +cpus_read_lock (); +on_each_cpu (vmx_tdx_on, &vmx_tdx, true);/* TDX requires vmxon. */ + r =atomic_read (&vmx_tdx.err); +if (!r) + r =tdx_module_setup (); +else + r = -EIO ; +on_each_cpu (vmx_off, &vmx_tdx.vmx_enabled, true); +cpus_read_unlock (); +free_cpumask_var (vmx_tdx.vmx_enabled); + +out: +return r; +}arch/x86/kvm/vmx/x86_ops.h +8
-0@@ -18,6 +18,8 @@ bool kvm_is_vmx_supported(void);int __init vmx_init (void );void vmx_exit (void ); +__init int vmx_hardware_setup (void ); +extern struct kvm_x86_ops vt_x86_ops__initdata ;extern struct kvm_x86_init_ops vt_init_ops__initdata ;@@ -133,4 +135,10 @@ void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu);#endif void vmx_setup_mce (struct kvm_vcpu *vcpu); +#ifdef CONFIG_INTEL_TDX_HOST +int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops); +#else +static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; } +#endif +#endif /* __KVM_X86_VMX_X86_OPS_H */
Re: [PATCH v17 011/116] KVM: TDX: Add C wrapper functions for SEAMCALLs to the TDX module
In reply to [email protected]Quoted 10 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
A VMM interacts with the TDX module using a new instruction (SEAMCALL). For instance, a TDX VMM does not have full access to the VM control structure corresponding to VMX VMCS. Instead, a VMM induces the TDX module to act on behalf via SEAMCALLs.
Export
__seamcalland define C wrapper functions for SEAMCALLs for readability.
Quoted 6 lines
Some SEAMCALL APIs donate host pages to TDX module or guest TD, and the donated pages are encrypted. Those require the VMM to flush the cache lines to avoid cache line alias.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>
Quoted 312 lines
---
v15 -> v16:
- use structtdx_module_argsinstead of structtdx_module_output
- Addtdh_mem_sept_rd()for SEPT_VE_DISABLE=1.--- arch/x86/include/asm/asm-prototypes.h | 1 + arch/x86/include/asm/tdx.h | 12 ++ arch/x86/kvm/vmx/tdx_ops.h | 226 ++++++++++++++++++++++++++ arch/x86/virt/vmx/tdx/seamcall.S | 4 + 4 files changed, 243 insertions(+)create mode 100644
arch/x86/kvm/vmx/tdx_ops.harch/x86/include/asm/asm-prototypes.h +1
-0@@ -6,6 +6,7 @@#include <asm/page.h> #include <asm/checksum.h> #include <asm/mce.h> +#include <asm/tdx.h> #include <asm-generic/asm-prototypes.h> arch/x86/include/asm/tdx.h +12
-0@@ -115,6 +115,18 @@ int tdx_enable(void);void tdx_reset_memory (void );bool tdx_is_private_mem (unsigned long phys);#else +static inline u64 __seamcall (u64 fn,struct tdx_module_args *args) +{ +return TDX_SEAMCALL_UD ; +} +static inline u64 __seamcall_ret (u64 fn,struct tdx_module_args *args) +{ +return TDX_SEAMCALL_UD ; +} +static inline u64 __seamcall_saved_ret (u64 fn,struct tdx_module_args *args) +{ +return TDX_SEAMCALL_UD ; +}static inline bool platform_tdx_enabled (void ) {return false; }static inline int tdx_cpu_enable (void ) {return -ENODEV ; }static inline int tdx_enable (void ) {return -ENODEV ; }arch/x86/kvm/vmx/tdx_ops.h new file +226
-0@@ -0,0 +1,226 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* constants/data definitions for TDX SEAMCALLs */ + +#ifndef __KVM_X86_TDX_OPS_H +#define __KVM_X86_TDX_OPS_H + +#include <linux/compiler.h> + +#include <asm/cacheflush.h> +#include <asm/asm.h> +#include <asm/kvm_host.h> + +#include "tdx_errno.h" +#include "tdx_arch.h" +#include "x86.h" + +static inline u64 tdx_seamcall (u64 op,u64 rcx,u64 rdx,u64 r8,u64 r9, +struct tdx_module_args *out) +{ +u64 ret; + +if (out) { + *out = (struct tdx_module_args ) { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall_ret (op, out); + }else { +struct tdx_module_args args = { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall (op, &args); + } +if (unlikely (ret ==TDX_SEAMCALL_UD )) { +/* +* SEAMCALLs fail with TDX_SEAMCALL_UD returned when VMX is off. +* This can happen when the host gets rebooted or live +* updated. In this case, the instruction execution is ignored +* as KVM is shut down, so the error code is suppressed. Other +* than this, the error is unexpected and the execution can't +* continue as the TDX features reply on VMX to be on. +*/ +kvm_spurious_fault (); +return 0 ; + } +return ret; +} + +static inline u64 tdh_mng_addcx (hpa_t tdr,hpa_t addr) +{ +clflush_cache_range (__va (addr),PAGE_SIZE ); +return tdx_seamcall (TDH_MNG_ADDCX , addr, tdr,0 ,0 ,NULL ); +} + +static inline u64 tdh_mem_page_add (hpa_t tdr,gpa_t gpa,hpa_t hpa,hpa_t source, +struct tdx_module_args *out) +{ +clflush_cache_range (__va (hpa),PAGE_SIZE ); +return tdx_seamcall (TDH_MEM_PAGE_ADD , gpa, tdr, hpa, source, out); +} + +static inline u64 tdh_mem_sept_add (hpa_t tdr,gpa_t gpa,int level,hpa_t page, +struct tdx_module_args *out) +{ +clflush_cache_range (__va (page),PAGE_SIZE ); +return tdx_seamcall (TDH_MEM_SEPT_ADD , gpa | level, tdr, page,0 , out); +} + +static inline u64 tdh_mem_sept_rd (hpa_t tdr,gpa_t gpa,int level, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MEM_SEPT_RD , gpa | level, tdr,0 ,0 , out); +} + +static inline u64 tdh_mem_sept_remove (hpa_t tdr,gpa_t gpa,int level, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MEM_SEPT_REMOVE , gpa | level, tdr,0 ,0 , out); +} + +static inline u64 tdh_vp_addcx (hpa_t tdvpr,hpa_t addr) +{ +clflush_cache_range (__va (addr),PAGE_SIZE ); +return tdx_seamcall (TDH_VP_ADDCX , addr, tdvpr,0 ,0 ,NULL ); +} + +static inline u64 tdh_mem_page_relocate (hpa_t tdr,gpa_t gpa,hpa_t hpa, +struct tdx_module_args *out) +{ +clflush_cache_range (__va (hpa),PAGE_SIZE ); +return tdx_seamcall (TDH_MEM_PAGE_RELOCATE , gpa, tdr, hpa,0 , out); +} + +static inline u64 tdh_mem_page_aug (hpa_t tdr,gpa_t gpa,hpa_t hpa, +struct tdx_module_args *out) +{ +clflush_cache_range (__va (hpa),PAGE_SIZE ); +return tdx_seamcall (TDH_MEM_PAGE_AUG , gpa, tdr, hpa,0 , out); +} + +static inline u64 tdh_mem_range_block (hpa_t tdr,gpa_t gpa,int level, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MEM_RANGE_BLOCK , gpa | level, tdr,0 ,0 , out); +} + +static inline u64 tdh_mng_key_config (hpa_t tdr) +{ +return tdx_seamcall (TDH_MNG_KEY_CONFIG , tdr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_create (hpa_t tdr,int hkid) +{ +clflush_cache_range (__va (tdr),PAGE_SIZE ); +return tdx_seamcall (TDH_MNG_CREATE , tdr, hkid,0 ,0 ,NULL ); +} + +static inline u64 tdh_vp_create (hpa_t tdr,hpa_t tdvpr) +{ +clflush_cache_range (__va (tdvpr),PAGE_SIZE ); +return tdx_seamcall (TDH_VP_CREATE , tdvpr, tdr,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_rd (hpa_t tdr,u64 field,struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MNG_RD , tdr, field,0 ,0 , out); +} + +static inline u64 tdh_mr_extend (hpa_t tdr,gpa_t gpa, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MR_EXTEND , gpa, tdr,0 ,0 , out); +} + +static inline u64 tdh_mr_finalize (hpa_t tdr) +{ +return tdx_seamcall (TDH_MR_FINALIZE , tdr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_vp_flush (hpa_t tdvpr) +{ +return tdx_seamcall (TDH_VP_FLUSH , tdvpr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_vpflushdone (hpa_t tdr) +{ +return tdx_seamcall (TDH_MNG_VPFLUSHDONE , tdr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_key_freeid (hpa_t tdr) +{ +return tdx_seamcall (TDH_MNG_KEY_FREEID , tdr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mng_init (hpa_t tdr,hpa_t td_params, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MNG_INIT , tdr, td_params,0 ,0 , out); +} + +static inline u64 tdh_vp_init (hpa_t tdvpr,u64 rcx) +{ +return tdx_seamcall (TDH_VP_INIT , tdvpr, rcx,0 ,0 ,NULL ); +} + +static inline u64 tdh_vp_rd (hpa_t tdvpr,u64 field, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_VP_RD , tdvpr, field,0 ,0 , out); +} + +static inline u64 tdh_mng_key_reclaimid (hpa_t tdr) +{ +return tdx_seamcall (TDH_MNG_KEY_RECLAIMID , tdr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_phymem_page_reclaim (hpa_t page, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_PHYMEM_PAGE_RECLAIM , page,0 ,0 ,0 , out); +} + +static inline u64 tdh_mem_page_remove (hpa_t tdr,gpa_t gpa,int level, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MEM_PAGE_REMOVE , gpa | level, tdr,0 ,0 , out); +} + +static inline u64 tdh_sys_lp_shutdown (void ) +{ +return tdx_seamcall (TDH_SYS_LP_SHUTDOWN ,0 ,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mem_track (hpa_t tdr) +{ +return tdx_seamcall (TDH_MEM_TRACK , tdr,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_mem_range_unblock (hpa_t tdr,gpa_t gpa,int level, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_MEM_RANGE_UNBLOCK , gpa | level, tdr,0 ,0 , out); +} + +static inline u64 tdh_phymem_cache_wb (bool resume) +{ +return tdx_seamcall (TDH_PHYMEM_CACHE_WB , resume ?1 :0 ,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_phymem_page_wbinvd (hpa_t page) +{ +return tdx_seamcall (TDH_PHYMEM_PAGE_WBINVD , page,0 ,0 ,0 ,NULL ); +} + +static inline u64 tdh_vp_wr (hpa_t tdvpr,u64 field,u64 val,u64 mask, +struct tdx_module_args *out) +{ +return tdx_seamcall (TDH_VP_WR , tdvpr, field, val, mask, out); +} + +#endif /* __KVM_X86_TDX_OPS_H */ arch/x86/virt/vmx/tdx/seamcall.S +4
-0@@ -1,5 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0 */ #include <linux/linkage.h> +#include <asm/export.h> #include <asm/frame.h> #include "tdxcall.S"@@ -21,6 +22,7 @@ SYM_FUNC_START(__seamcall) TDX_MODULE_CALL host=1 SYM_FUNC_END(__seamcall) +EXPORT_SYMBOL_GPL(__seamcall) /* * __seamcall_ret() - Host-side interface functions to SEAM software@@ -40,6 +42,7 @@ SYM_FUNC_END(__seamcall) SYM_FUNC_START(__seamcall_ret) TDX_MODULE_CALL host=1 ret=1 SYM_FUNC_END(__seamcall_ret) +EXPORT_SYMBOL_GPL(__seamcall_ret) /* * __seamcall_saved_ret() - Host-side interface functions to SEAM software@@ -59,3 +62,4 @@ SYM_FUNC_END(__seamcall_ret) SYM_FUNC_START(__seamcall_saved_ret) TDX_MODULE_CALL host=1 ret=1 saved=1 SYM_FUNC_END(__seamcall_saved_ret) +EXPORT_SYMBOL_GPL(__seamcall_saved_ret)
Re: [PATCH v17 012/116] KVM: TDX: Retry SEAMCALL on the lack of entropy error
In reply to [email protected]Quoted 52 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Some SEAMCALL may return
TDX_RND_NO_ENTROPYerror when the entropy is lacking. Retry SEAMCALL on the error followingrdrand_long()to retryRDRAND_RETRY_LOOPStimes.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx_errno.h | 1 + arch/x86/kvm/vmx/tdx_ops.h | 40 +++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 17 deletions(-)arch/x86/kvm/vmx/tdx_errno.h +1
-0@@ -14,6 +14,7 @@#define TDX_OPERAND_INVALID 0xC000010000000000ULL#define TDX_OPERAND_BUSY 0x8000020000000000ULL#define TDX_PREVIOUS_TLB_EPOCH_BUSY 0x8000020100000000ULL +#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL#define TDX_VCPU_NOT_ASSOCIATED 0x8000070200000000ULL#define TDX_KEY_GENERATION_FAILED 0x8000080000000000ULL#define TDX_KEY_STATE_INCORRECT 0xC000081100000000ULLarch/x86/kvm/vmx/tdx_ops.h +2
-9@@ -6,6 +6,7 @@#include <linux/compiler.h> +#include <asm/archrandom.h> #include <asm/cacheflush.h> #include <asm/asm.h> #include <asm/kvm_host.h> @@ -17,25 +18,30 @@static inline u64 tdx_seamcall (u64 op,u64 rcx,u64 rdx,u64 r8,u64 r9,struct tdx_module_args *out) { +int retry;u64 ret; -if (out) { - *out = (struct tdx_module_args ) { - .rcx = rcx, - .rdx = rdx, - .r8 = r8, - .r9 = r9, - }; - ret =__seamcall_ret (op, out); - }else {
arch/x86/kvm/vmx/tdx_ops.h line 25
Quoted 10 lines
arch/x86/kvm/vmx/tdx_ops.h +2
-8-struct tdx_module_args args = { - .rcx = rcx, - .rdx = rdx, - .r8 = r8, - .r9 = r9, - }; - ret =__seamcall (op, &args); - } +/* Mimic the existing rdrand_long() to retry RDRAND_RETRY_LOOPS times. */ + retry =RDRAND_RETRY_LOOPS ;
Nit: The value can be assigned when define.
Quoted 22 lines
arch/x86/kvm/vmx/tdx_ops.h +19
-0+do { +if (out) { + *out = (struct tdx_module_args ) { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall_ret (op, out); + }else { +struct tdx_module_args args = { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall (op, &args); + } + }while (unlikely (ret ==TDX_RND_NO_ENTROPY ) && --retry);if (unlikely (ret ==TDX_SEAMCALL_UD )) {/* * SEAMCALLs fail with TDX_SEAMCALL_UD returned when VMX is off.
Re: [PATCH v17 013/116] KVM: TDX: Add helper functions to print TDX SEAMCALL error
In reply to [email protected]Quoted 4 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Add helper functions to print out errors from the TDX module in a uniform manner.
Quoted 71 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/vmx/tdx_error.c | 20 ++++++++++++++++++++ arch/x86/kvm/vmx/tdx_ops.h | 5 +++++ 3 files changed, 26 insertions(+), 1 deletion(-)create mode 100644
arch/x86/kvm/vmx/tdx_error.carch/x86/kvm/Makefile +1
-1@@ -25,7 +25,7 @@ kvm-$(CONFIG_KVM_SMM) += smm.o kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \ vmx/hyperv.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 +kvm-intel-$(CONFIG_INTEL_TDX_HOST) += vmx/tdx.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.o svm/hyperv.oarch/x86/kvm/vmx/tdx_error.c new file +20
-0@@ -0,0 +1,20 @@ +// 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_args *out) +{ +if (!out) { +pr_err_ratelimited ("SEAMCALL[%lld] failed: 0x%llx\n" , + op, error_code); +return ; + } + +#define MSG "SEAMCALL[%lld] failed: 0x%llx RCX 0x%llx RDX 0x%llx R8 0x%llx R9 0x%llx R10 0x%llx R11 0x%llx\n" +pr_err_ratelimited (MSG , op, error_code, out->rcx, out->rdx, out->r8, + out->r9, out->r10, out->r11); +}arch/x86/kvm/vmx/tdx_ops.h +5
-0@@ -10,6 +10,7 @@#include <asm/cacheflush.h> #include <asm/asm.h> #include <asm/kvm_host.h> +#include <asm/tdx.h> #include "tdx_errno.h" #include "tdx_arch.h" @@ -57,6 +58,10 @@ static inline u64 tdx_seamcall(u64 op, u64 rcx, u64 rdx, u64 r8, u64 r9,return ret; } +#ifdef CONFIG_INTEL_TDX_HOST +void pr_tdx_error (u64 op,u64 error_code,const struct tdx_module_args *out); +#endif +static inline u64 tdh_mng_addcx (hpa_t tdr,hpa_t addr) {clflush_cache_range (__va (addr),PAGE_SIZE );
Re: [PATCH v17 016/116] x86/virt/tdx: Add a helper function to return system wide info about TDX module
In reply to [email protected]Quoted 7 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX KVM needs system-wide information about the TDX module, struct
tdsysinfo_struct. Add a helper functiontdx_get_sysinfo()to return it instead of KVM getting it with various error checks. Make KVM call the function and stash the info. Move out the struct definition about it to common placearch/x86/include/asm/tdx.h.
Why add tdx_get_sysinfo() to arch/x86/virt/vmx/tdx/tdx.c?
Seems throughout the whole patchset, tdx_get_sysinfo() is only called inside arch/x86/kvm/vmx/tdx.c, even no need to add the helper API since it can be reference in arch/x86/kvm/vmx/tdx.c directly?
Quoted 228 lines
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/tdx.h | 59 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.c | 15 +++++++++- arch/x86/virt/vmx/tdx/tdx.c | 20 +++++++++++-- arch/x86/virt/vmx/tdx/tdx.h | 51 -------------------------------- 4 files changed, 91 insertions(+), 54 deletions(-)arch/x86/include/asm/tdx.h +59
-0@@ -109,6 +109,62 @@ static inline u64 sc_retry(sc_func_t func, u64 fn,#define seamcall_ret (_fn, _args) sc_retry(__seamcall_ret, (_fn), (_args))#define seamcall_saved_ret (_fn, _args) sc_retry(__seamcall_saved_ret, (_fn), (_args)) +struct tdx_cpuid_config { + __struct_group(tdx_cpuid_config_leaf, leaf_sub_leaf, __packed, + u32 leaf; +u32 sub_leaf; + ); + __struct_group(tdx_cpuid_config_value, value, __packed, + u32 eax; +u32 ebx; +u32 ecx; +u32 edx; + ); +} __packed; + +#define TDSYSINFO_STRUCT_SIZE 1024 +#define TDSYSINFO_STRUCT_ALIGNMENT 1024 + +/* +* The size of this structure itself is flexible. The actual structure +* passed to TDH.SYS.INFO must be padded to TDSYSINFO_STRUCT_SIZE bytes +* and TDSYSINFO_STRUCT_ALIGNMENT bytes aligned. +*/ +struct tdsysinfo_struct { +/* TDX-SEAM Module Info */ +u32 attributes; +u32 vendor_id; +u32 build_date; +u16 build_num; +u16 minor_version; +u16 major_version; +u8 reserved0[14 ]; +/* Memory Info */ +u16 max_tdmrs; +u16 max_reserved_per_tdmr; +u16 pamt_entry_size; +u8 reserved1[10 ]; +/* Control Struct Info */ +u16 tdcs_base_size; +u8 reserved2[2 ]; +u16 tdvps_base_size; +u8 tdvps_xfam_dependent_size; +u8 reserved3[9 ]; +/* TD Capabilities */ +u64 attributes_fixed0; +u64 attributes_fixed1; +u64 xfam_fixed0; +u64 xfam_fixed1; +u8 reserved4[32 ]; +u32 num_cpuid_config; +/* +* The actual number of CPUID_CONFIG depends on above +* 'num_cpuid_config'. +*/ + DECLARE_FLEX_ARRAY(struct tdx_cpuid_config, cpuid_configs); +} __packed; + +const struct tdsysinfo_struct *tdx_get_sysinfo (void );bool platform_tdx_enabled (void );int tdx_cpu_enable (void );int tdx_enable (void );@@ -137,6 +193,9 @@ static inline u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args) {return TDX_SEAMCALL_UD ; } + +struct tdsysinfo_struct ; +static inline const struct tdsysinfo_struct *tdx_get_sysinfo (void ) {return NULL ; }static inline bool platform_tdx_enabled (void ) {return false; }static inline int tdx_cpu_enable (void ) {return -ENODEV ; }static inline int tdx_enable (void ) {return -ENODEV ; }arch/x86/kvm/vmx/tdx.c +14
-1@@ -11,9 +11,18 @@#undef pr_fmt#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#define TDX_MAX_NR_CPUID_CONFIGS \ + ((TDSYSINFO_STRUCT_SIZE - \ + offsetof(struct tdsysinfo_struct, cpuid_configs)) \ + / sizeof(struct tdx_cpuid_config)) +static int __init tdx_module_setup (void ) { -int ret; +const struct tdsysinfo_struct *tdsysinfo; +int ret =0 ; + +BUILD_BUG_ON (sizeof (*tdsysinfo) >TDSYSINFO_STRUCT_SIZE ); +BUILD_BUG_ON (TDX_MAX_NR_CPUID_CONFIGS !=37 ); ret =tdx_enable ();if (ret) {@@ -21,6 +30,10 @@ static int __init tdx_module_setup(void)return ret; } +/* Sanitary check just in case. */ + tdsysinfo =tdx_get_sysinfo (); +WARN_ON (tdsysinfo->num_cpuid_config >TDX_MAX_NR_CPUID_CONFIGS ); +return 0 ; }arch/x86/virt/vmx/tdx/tdx.c +18
-2@@ -265,6 +265,20 @@ static int get_tdx_sysinfo(struct tdsysinfo_struct *tdsysinfo,return 0 ; } +static struct tdsysinfo_struct *tdsysinfo; + +const struct tdsysinfo_struct *tdx_get_sysinfo (void ) +{ +const struct tdsysinfo_struct *r =NULL ; + +mutex_lock (&tdx_module_lock); +if (tdx_module_status ==TDX_MODULE_INITIALIZED ) + r = tdsysinfo; +mutex_unlock (&tdx_module_lock); +return r; +} +EXPORT_SYMBOL_GPL (tdx_get_sysinfo); +/* * Add a memory region as a TDX memory block. The caller must make sure * all memory regions are added in address ascending order and don't @@ -1090,7 +1104,6 @@ static int init_tdmrs(struct tdmr_info_list *tdmr_list)static int init_tdx_module (void ) { -struct tdsysinfo_struct *tdsysinfo;struct cmr_info *cmr_array;int tdsysinfo_size;int cmr_array_size;@@ -1181,7 +1194,10 @@ static int init_tdx_module(void)* For now both @sysinfo and @cmr_array are only used during * module initialization, so always free them. */ -kfree (tdsysinfo); +if (ret) { +kfree (tdsysinfo); + tdsysinfo =NULL ; + }kfree (cmr_array);return ret;arch/x86/virt/vmx/tdx/tdx.h +0
-51@@ -36,57 +36,6 @@ struct cmr_info {#define MAX_CMRS 32#define CMR_INFO_ARRAY_ALIGNMENT 512 -struct cpuid_config { -u32 leaf; -u32 sub_leaf; -u32 eax; -u32 ebx; -u32 ecx; -u32 edx; -} __packed; - -#define TDSYSINFO_STRUCT_SIZE 1024 -#define TDSYSINFO_STRUCT_ALIGNMENT 1024 - -/* -* The size of this structure itself is flexible. The actual structure -* passed to TDH.SYS.INFO must be padded to TDSYSINFO_STRUCT_SIZE bytes -* and TDSYSINFO_STRUCT_ALIGNMENT bytes aligned. -*/ -struct tdsysinfo_struct { -/* TDX-SEAM Module Info */ -u32 attributes; -u32 vendor_id; -u32 build_date; -u16 build_num; -u16 minor_version; -u16 major_version; -u8 reserved0[14 ]; -/* Memory Info */ -u16 max_tdmrs; -u16 max_reserved_per_tdmr; -u16 pamt_entry_size; -u8 reserved1[10 ]; -/* Control Struct Info */ -u16 tdcs_base_size; -u8 reserved2[2 ]; -u16 tdvps_base_size; -u8 tdvps_xfam_dependent_size; -u8 reserved3[9 ]; -/* TD Capabilities */ -u64 attributes_fixed0; -u64 attributes_fixed1; -u64 xfam_fixed0; -u64 xfam_fixed1; -u8 reserved4[32 ]; -u32 num_cpuid_config; -/* -* The actual number of CPUID_CONFIG depends on above -* 'num_cpuid_config'. -*/ - DECLARE_FLEX_ARRAY(struct cpuid_config, cpuid_configs); -} __packed; -struct tdmr_reserved_area {u64 offset;u64 size;
Re: [PATCH v17 018/116] KVM: TDX: x86: Add ioctl to get TDX systemwide parameters
In reply to [email protected]tools/arch/x86/include/uapi/asm/kvm.h
Quoted 144 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Sean Christopherson <[email protected]>
Implement an ioctl to get system-wide parameters for TDX. Although the function is systemwide, vm scoped
mem_encioctl works for userspace VMM like qemu and device scoped version is not define, re-use vm scopedmem_enc.Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
v14 -> v15:
- ABI change: addedsupported_gpawand reserved area,--- arch/x86/include/uapi/asm/kvm.h | 24 ++++++++++ arch/x86/kvm/vmx/tdx.c | 64 +++++++++++++++++++++++++++ tools/arch/x86/include/uapi/asm/kvm.h | 52 ++++++++++++++++++++++ 3 files changed, 140 insertions(+)arch/x86/include/uapi/asm/kvm.h +24
-0@@ -593,4 +593,28 @@ struct kvm_tdx_cmd { __u64 error; }; +struct kvm_tdx_cpuid_config { +__u32 leaf; +__u32 sub_leaf; +__u32 eax; +__u32 ebx; +__u32 ecx; +__u32 edx; +}; + +struct kvm_tdx_capabilities { +__u64 attrs_fixed0; +__u64 attrs_fixed1; +__u64 xfam_fixed0; +__u64 xfam_fixed1; +#define TDX_CAP_GPAW_48 (1 << 0) +#define TDX_CAP_GPAW_52 (1 << 1) +__u32 supported_gpaw; +__u32 padding; +__u64 reserved[251 ]; + +__u32 nr_cpuid_configs; +struct kvm_tdx_cpuid_config cpuid_configs[]; +}; +#endif /* _ASM_X86_KVM_H */ arch/x86/kvm/vmx/tdx.c +64
-0@@ -6,6 +6,7 @@#include "capabilities.h" #include "x86_ops.h" #include "x86.h" +#include "mmu.h" #include "tdx.h" #undef pr_fmt@@ -16,6 +17,66 @@ offsetof(struct tdsysinfo_struct, cpuid_configs)) \ / sizeof(struct tdx_cpuid_config)) +static int tdx_get_capabilities (struct kvm_tdx_cmd *cmd) +{ +struct kvm_tdx_capabilities __user *user_caps; +const struct tdsysinfo_struct *tdsysinfo; +struct kvm_tdx_capabilities *caps =NULL ; +int ret =0 ; + +BUILD_BUG_ON (sizeof (struct kvm_tdx_cpuid_config ) != +sizeof (struct tdx_cpuid_config )); + +if (cmd->flags) +return -EINVAL ; + + tdsysinfo =tdx_get_sysinfo (); +if (!tdsysinfo) +return -EOPNOTSUPP ; + + caps =kmalloc (sizeof (*caps),GFP_KERNEL ); +if (!caps) +return -ENOMEM ; + + user_caps = (void __user *)cmd->data; +if (copy_from_user (caps, user_caps,sizeof (*caps))) { + ret = -EFAULT ; + goto out; + } + +if (caps->nr_cpuid_configs < tdsysinfo->num_cpuid_config) { + ret = -E2BIG ; + goto out; + } + + *caps = (struct kvm_tdx_capabilities ) { + .attrs_fixed0 = tdsysinfo->attributes_fixed0, + .attrs_fixed1 = tdsysinfo->attributes_fixed1, + .xfam_fixed0 = tdsysinfo->xfam_fixed0, + .xfam_fixed1 = tdsysinfo->xfam_fixed1, + .supported_gpaw =TDX_CAP_GPAW_48 | + (kvm_get_shadow_phys_bits () >=52 && +cpu_has_vmx_ept_5levels ()) ?TDX_CAP_GPAW_52 :0 , + .nr_cpuid_configs = tdsysinfo->num_cpuid_config, + .padding =0 , + }; + +if (copy_to_user (user_caps, caps,sizeof (*caps))) { + ret = -EFAULT ; + goto out; + } +if (copy_to_user (user_caps->cpuid_configs, &tdsysinfo->cpuid_configs, + tdsysinfo->num_cpuid_config * +sizeof (struct tdx_cpuid_config ))) { + ret = -EFAULT ; + } + +out: +/* kfree() accepts NULL. */ +kfree (caps); +return ret; +} +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_tdx_cmd tdx_cmd;@@ -29,6 +90,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)mutex_lock (&kvm->lock);switch (tdx_cmd.id) { +case KVM_TDX_CAPABILITIES : + r =tdx_get_capabilities (&tdx_cmd); +break ;default : r = -EINVAL ; goto out;tools/arch/x86/include/uapi/asm/kvm.h +0
-0
According to the discussion, Sean's preference is "never update KVM's uapi headers in tools/ in KVM's tree"
https://lore.kernel.org/all/Y8bZ%2FJ98V5i3wG%[email protected]/
Quoted 57 lines
tools/arch/x86/include/uapi/asm/kvm.h +52
-0@@ -562,4 +562,56 @@ struct kvm_pmu_event_filter {/* x86-specific KVM_EXIT_HYPERCALL flags. */ #define KVM_EXIT_HYPERCALL_LONG_MODE BIT(0) +/* Trust Domain eXtension sub-ioctl() commands. */ +enum kvm_tdx_cmd_id { +KVM_TDX_CAPABILITIES =0 , + +KVM_TDX_CMD_NR_MAX , +}; + +struct kvm_tdx_cmd { +/* enum kvm_tdx_cmd_id */ +__u32 id; +/* flags for sub-commend. If sub-command doesn't use this, set zero. */ +__u32 flags; +/* +* data for each sub-command. An immediate or a pointer to the actual +* data in process virtual address. If sub-command doesn't use it, +* set zero. +*/ +__u64 data; +/* +* Auxiliary error code. The sub-command may return TDX SEAMCALL +* status code in addition to -Exxx. +* Defined for consistency with struct kvm_sev_cmd. +*/ +__u64 error; +/* Reserved: Defined for consistency with struct kvm_sev_cmd. */ +__u64 unused; +}; + +struct kvm_tdx_cpuid_config { +__u32 leaf; +__u32 sub_leaf; +__u32 eax; +__u32 ebx; +__u32 ecx; +__u32 edx; +}; + +struct kvm_tdx_capabilities { +__u64 attrs_fixed0; +__u64 attrs_fixed1; +__u64 xfam_fixed0; +__u64 xfam_fixed1; +#define TDX_CAP_GPAW_48 (1 << 0) +#define TDX_CAP_GPAW_52 (1 << 1) +__u32 supported_gpaw; +__u32 padding; +__u64 reserved[251 ]; + +__u32 nr_cpuid_configs; +struct kvm_tdx_cpuid_config cpuid_configs[]; +}; +#endif /* _ASM_X86_KVM_H */
Re: [PATCH v17 093/116] KVM: TDX: Handle TDX PV port io hypercall
In reply to [email protected]Quoted 61 lines · On Tue, Nov 7, 2023 at 6:58 AM <[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]>Reviewed-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)arch/x86/kvm/vmx/tdx.c +42
-0@@ -1140,6 +1140,61 @@ 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_ONCE (vcpu->arch.pio.count !=1 ); + + ret = ctxt->ops->pio_in_emulated (ctxt, vcpu->arch.pio.size, + vcpu->arch.pio.port, &val,1 ); +WARN_ON_ONCE (!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; +bool write; + + ++vcpu->stat.io_exits; + + size =tdvmcall_a0_read (vcpu); + write =tdvmcall_a1_read (vcpu); + port =tdvmcall_a2_read (vcpu); + +if (size !=1 && size !=2 && size !=4 ) { +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_INVALID_OPERAND ); +return 1 ; + } + +if (write) { + val =tdvmcall_a3_read (vcpu); + ret = ctxt->ops->pio_out_emulated (ctxt, size, port, &val,1 ); + +/* No need for a complete_userspace_io callback. */
arch/x86/kvm/vmx/tdx.c static int tdx_emulate_hlt(struct kvm_vcpu *vcpu) · line 1194
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ vcpu->arch.pio.count =0 ; + }else { + 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); + } +if (ret) +tdvmcall_set_return_code (vcpu,TDG_VP_VMCALL_SUCCESS );
In the case of a write operation that needs to exit to userspace, pio_out_emulated is going to return 0 which means that the return code is never set by tdx_emulate_io.
The reason this code is working now is because r10 is already set to 0 by the guest as part of the call to tdvmcall and never gets written by the host.
Quoted 18 lines
arch/x86/kvm/vmx/tdx.c +5
-1+return ret; +} +static int handle_tdvmcall (struct kvm_vcpu *vcpu) {if (tdvmcall_exit_type (vcpu))@@ -1150,6 +1205,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 ; } --2.25.1
Re: [PATCH v17 019/116] KVM: x86, tdx: Make KVM_CAP_MAX_VCPUS backend specific
In reply to [email protected]Quoted 6 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata<[email protected]>
TDX has its own limitation on the maximum number of vcpus that the guest can accommodate. Allow x86 kvm backend to implement its own
KVM_ENABLE_CAPhandler and implement TDX backend forKVM_CAP_MAX_VCPUS. user space VMM, e.g. qemu, can specify its value instead ofKVM_MAX_VCPUS.
Should use "TDX" instread of "x86, tdx" in shortlog?
Quoted 51 lines
Signed-off-by Isaku Yamahata<[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 2 ++ arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/vmx/main.c | 22 ++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.c | 30 ++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 3 +++ arch/x86/kvm/vmx/x86_ops.h | 5 +++++ arch/x86/kvm/x86.c | 4 ++++ 7 files changed, 68 insertions(+)arch/x86/include/asm/kvm-x86-ops.h +2
-0@@ -21,6 +21,8 @@ KVM_X86_OP(hardware_unsetup) KVM_X86_OP(has_emulated_msr) KVM_X86_OP(vcpu_after_set_cpuid) KVM_X86_OP(is_vm_type_supported) +KVM_X86_OP_OPTIONAL(max_vcpus); +KVM_X86_OP_OPTIONAL(vm_enable_cap) KVM_X86_OP(vm_init) KVM_X86_OP_OPTIONAL(vm_destroy) KVM_X86_OP_OPTIONAL_RET0(vcpu_precreate)arch/x86/include/asm/kvm_host.h +2
-0@@ -1574,7 +1574,9 @@ struct kvm_x86_ops {void (*vcpu_after_set_cpuid)(struct kvm_vcpu *vcpu);bool (*is_vm_type_supported)(unsigned long vm_type); +int (*max_vcpus)(struct kvm *kvm);unsigned int vm_size; +int (*vm_enable_cap)(struct kvm *kvm,struct kvm_enable_cap *cap);int (*vm_init)(struct kvm *kvm);void (*vm_destroy)(struct kvm *kvm);arch/x86/kvm/vmx/main.c +1
-0@@ -6,6 +6,7 @@#include "nested.h" #include "pmu.h" #include "tdx.h" +#include "tdx_arch.h" static bool enable_tdx__ro_after_init ;module_param_named (tdx, enable_tdx, bool,0444 );
arch/x86/kvm/vmx/main.c static bool vt_is_vm_type_supported(unsigned long type) · line 26
Quoted 10 lines
arch/x86/kvm/vmx/main.c +7
-0@@ -16,6 +17,17 @@ static bool vt_is_vm_type_supported(unsigned long type) (enable_tdx &&tdx_is_vm_type_supported (type)); } +static int vt_max_vcpus (struct kvm *kvm) +{ +if (!kvm) +return KVM_MAX_VCPUS ; + +if (is_td (kvm)) +return min3 (kvm->max_vcpus,KVM_MAX_VCPUS ,TDX_MAX_VCPUS );
kvm->max_vcpus is initialized to KVM_MAX_VCPUS when kvm_create_vm(). Also, when userspace tries to set the value, it's also limited by KVM_MAX_VCPUS. So, for x86, the value is <= KVM_MAX_VCPUS for sure.
It could be simpler:
+ if (is_td(kvm))
+ return min(kvm->max_vcpus, TDX_MAX_VCPUS);
Quoted 42 lines
arch/x86/kvm/vmx/main.c +14
-0+ +return kvm->max_vcpus; +} +static int vt_hardware_enable (void ) {int ret;@@ -43,6 +55,14 @@ static __init int vt_hardware_setup(void)return 0 ; } +static int vt_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +if (is_td (kvm)) +return tdx_vm_enable_cap (kvm, cap); + +return -EINVAL ; +} +static int vt_vm_init (struct kvm *kvm) {if (is_td (kvm))@@ -80,7 +100,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .has_emulated_msr = vmx_has_emulated_msr, .is_vm_type_supported = vt_is_vm_type_supported, + .max_vcpus = vt_max_vcpus, .vm_size = sizeof(struct kvm_vmx), + .vm_enable_cap = vt_vm_enable_cap, .vm_init = vt_vm_init, .vm_destroy = vmx_vm_destroy,arch/x86/kvm/vmx/tdx.c +2
-0@@ -17,6 +17,36 @@ offsetof(struct tdsysinfo_struct, cpuid_configs)) \ / sizeof(struct tdx_cpuid_config)) +int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{
arch/x86/kvm/vmx/tdx.c line 31
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+int r; + +switch (cap->cap) { +case KVM_CAP_MAX_VCPUS : { +if (cap->flags || cap->args[0 ] ==0 ) +return -EINVAL ; +if (cap->args[0 ] >KVM_MAX_VCPUS ) +return -E2BIG ; +if (cap->args[0 ] >TDX_MAX_VCPUS ) +return -E2BIG ;
Can merge the two if statements into one.
+ if (cap->args[0] > KVM_MAX_VCPUS || cap->args[0] > TDX_MAX_VCPUS) + return -E2BIG;
Quoted 78 lines
arch/x86/kvm/vmx/tdx.c +18
-0+ +mutex_lock (&kvm->lock); +if (kvm->created_vcpus) + r = -EBUSY ; +else { + kvm->max_vcpus = cap->args[0 ]; + r =0 ; + } +mutex_unlock (&kvm->lock); +break ; + } +default : + r = -EINVAL ; +break ; + } +return r; +} +static int tdx_get_capabilities (struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;arch/x86/kvm/vmx/tdx.h +3
-0@@ -3,6 +3,9 @@#define __KVM_X86_TDX_H #ifdef CONFIG_INTEL_TDX_HOST + +#include "tdx_ops.h" + struct kvm_tdx { struct kvm kvm; /* TDX specific members follow. */arch/x86/kvm/vmx/x86_ops.h +5
-0@@ -138,11 +138,16 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);bool tdx_is_vm_type_supported (unsigned long type); +int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap);int tdx_vm_ioctl (struct kvm *kvm,void __user *argp);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; } +static inline int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) +{ +return -EINVAL ; +};static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; }#endif arch/x86/kvm/x86.c +4
-0@@ -4694,6 +4694,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)break ;case KVM_CAP_MAX_VCPUS : r =KVM_MAX_VCPUS ; +if (kvm_x86_ops.max_vcpus) + r =static_call (kvm_x86_max_vcpus)(kvm);break ;case KVM_CAP_MAX_VCPU_ID : r =KVM_MAX_VCPU_IDS ;@@ -6641,6 +6643,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,break ;default : r = -EINVAL ; +if (kvm_x86_ops.vm_enable_cap) + r =static_call (kvm_x86_vm_enable_cap)(kvm, cap);break ; }return r;
Re: [PATCH v17 016/116] x86/virt/tdx: Add a helper function to return system wide info about TDX module
In reply to [email protected]Quoted 71 lines · Isaku
From: Isaku Yamahata <[email protected]>
TDX KVM needs system-wide information about the TDX module, struct
tdsysinfo_struct. Add a helper functiontdx_get_sysinfo()to return it instead of KVM getting it with various error checks. Make KVM call the function and stash the info. Move out the struct definition about it to common placearch/x86/include/asm/tdx.h.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/include/asm/tdx.h | 59 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.c | 15 +++++++++- arch/x86/virt/vmx/tdx/tdx.c | 20 +++++++++++-- arch/x86/virt/vmx/tdx/tdx.h | 51 -------------------------------- 4 files changed, 91 insertions(+), 54 deletions(-)arch/x86/include/asm/tdx.h +47
-0@@ -109,6 +109,62 @@ static inline u64 sc_retry(sc_func_t func, u64 fn,#define seamcall_ret (_fn, _args) sc_retry(__seamcall_ret, (_fn), (_args))#define seamcall_saved_ret (_fn, _args) sc_retry(__seamcall_saved_ret, (_fn), (_args)) +struct tdx_cpuid_config { + __struct_group(tdx_cpuid_config_leaf, leaf_sub_leaf, __packed, + u32 leaf; +u32 sub_leaf; + ); + __struct_group(tdx_cpuid_config_value, value, __packed, + u32 eax; +u32 ebx; +u32 ecx; +u32 edx; + ); +} __packed; + +#define TDSYSINFO_STRUCT_SIZE 1024 +#define TDSYSINFO_STRUCT_ALIGNMENT 1024 + +/* +* The size of this structure itself is flexible. The actual structure +* passed to TDH.SYS.INFO must be padded to TDSYSINFO_STRUCT_SIZE bytes +* and TDSYSINFO_STRUCT_ALIGNMENT bytes aligned. +*/ +struct tdsysinfo_struct { +/* TDX-SEAM Module Info */ +u32 attributes; +u32 vendor_id; +u32 build_date; +u16 build_num; +u16 minor_version; +u16 major_version; +u8 reserved0[14 ]; +/* Memory Info */ +u16 max_tdmrs; +u16 max_reserved_per_tdmr; +u16 pamt_entry_size; +u8 reserved1[10 ]; +/* Control Struct Info */ +u16 tdcs_base_size; +u8 reserved2[2 ]; +u16 tdvps_base_size; +u8 tdvps_xfam_dependent_size; +u8 reserved3[9 ]; +/* TD Capabilities */ +u64 attributes_fixed0; +u64 attributes_fixed1; +u64 xfam_fixed0; +u64 xfam_fixed1; +u8 reserved4[32 ];
arch/x86/include/asm/tdx.h static inline u64 sc_retry(sc_func_t func, u64 fn, · line 168
Quoted 10 lines
arch/x86/include/asm/tdx.h +9
-0+u32 num_cpuid_config; +/* +* The actual number of CPUID_CONFIG depends on above +* 'num_cpuid_config'. +*/ + DECLARE_FLEX_ARRAY(struct tdx_cpuid_config, cpuid_configs); +} __packed; + +const struct tdsysinfo_struct *tdx_get_sysinfo (void );
With the lastest TDX host code I don't think we need TDSYSINFO_STRUCT anymore.
I think it makes more sense to modify the TDX host code to expose an infrastructure to read TDX module global metadata and KVM can just use that to get what it wants to create/run TDX guests.
I am working on these patches and I'll update to you when done (should be soon).
Re: [PATCH v17 020/116] KVM: TDX: create/destroy VM structure
In reply to [email protected]Quoted 85 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
As the first step to create TDX guest, create/destroy VM struct. Assign TDX private Host Key ID (HKID) to the TDX guest for memory encryption and allocate extra pages for the TDX guest. On destruction, free allocated pages, and HKID.
Before tearing down private page tables, TDX requires some resources of the guest TD to be destroyed (i.e. HKID must have been reclaimed, etc). Add mmu notifier release callback before tearing down private page tables for it.
Add
vm_free()ofkvm_x86_opshook at the end ofkvm_arch_destroy_vm()because some per-VM TDX resources, e.g. TDR, need to be freed after other TDX resources, e.g. HKID, were freed.Co-developed-by Kai Huang <[email protected]>Signed-off-by Kai Huang <[email protected]>Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
v16:
- Simplifiedtdx_reclaim_page()
- Reorganize the locking oftdx_release_hkid(), and usesmp_call_mask()instead ofsmp_call_on_cpu()to hold spinlock to race with invalidation on releasing guest memfd--- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/Kconfig | 2 + arch/x86/kvm/mmu/mmu.c | 7 + arch/x86/kvm/vmx/main.c | 35 ++- arch/x86/kvm/vmx/tdx.c | 471 ++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 6 +- arch/x86/kvm/vmx/x86_ops.h | 8 + arch/x86/kvm/x86.c | 1 + 9 files changed, 528 insertions(+), 6 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +2
-0@@ -24,7 +24,9 @@ KVM_X86_OP(is_vm_type_supported)KVM_X86_OP_OPTIONAL (max_vcpus); KVM_X86_OP_OPTIONAL(vm_enable_cap) KVM_X86_OP(vm_init) +KVM_X86_OP_OPTIONAL(flush_shadow_all_private) KVM_X86_OP_OPTIONAL(vm_destroy) +KVM_X86_OP_OPTIONAL(vm_free) KVM_X86_OP_OPTIONAL_RET0(vcpu_precreate) KVM_X86_OP(vcpu_create) KVM_X86_OP(vcpu_free)arch/x86/include/asm/kvm_host.h +2
-0@@ -1578,7 +1578,9 @@ struct kvm_x86_ops {unsigned int vm_size;int (*vm_enable_cap)(struct kvm *kvm,struct kvm_enable_cap *cap);int (*vm_init)(struct kvm *kvm); +void (*flush_shadow_all_private)(struct kvm *kvm);void (*vm_destroy)(struct kvm *kvm); +void (*vm_free)(struct kvm *kvm);/* Create, but do not attach this VCPU */ int (*vcpu_precreate)(struct kvm *kvm);arch/x86/kvm/Kconfig +2
-0@@ -92,6 +92,8 @@ config KVM_SW_PROTECTED_VM config KVM_INTEL tristate "KVM for Intel (and compatible) processors support" depends on KVM && IA32_FEAT_CTL + select KVM_SW_PROTECTED_VM if INTEL_TDX_HOST + select KVM_PRIVATE_MEM if INTEL_TDX_HOST help Provides support for KVM on processors equipped with Intel's VT extensions, a.k.a. Virtual Machine Extensions (VMX).arch/x86/kvm/mmu/mmu.c +0
-0
arch/x86/kvm/mmu/mmu.c static void kvm_mmu_zap_all(struct kvm *kvm) · line 6804
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +7
-0@@ -6795,6 +6795,13 @@ static void kvm_mmu_zap_all(struct kvm *kvm)void kvm_arch_flush_shadow_all (struct kvm *kvm) { +/* +* kvm_mmu_zap_all() zaps both private and shared page tables. Before +* tearing down private page tables, TDX requires some TD resources to +* be destroyed (i.e. keyID must have been reclaimed, etc). Invoke +* kvm_x86_flush_shadow_all_private() for this. +*/ +static_call_cond (kvm_x86_flush_shadow_all_private)(kvm);
This Op name seems it related to flush pagetables, but actually it not. Maybe "flush_shadow_all_prepare" or other name having less confusion?
Quoted 235 lines
arch/x86/kvm/mmu/mmu.c +0
-0kvm_mmu_zap_all (kvm); }arch/x86/kvm/vmx/main.c +32
-3@@ -63,14 +63,41 @@ static int vt_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)return -EINVAL ; } +static void vt_hardware_unsetup (void ) +{ +if (enable_tdx) +tdx_hardware_unsetup (); +vmx_hardware_unsetup (); +} +static int vt_vm_init (struct kvm *kvm) {if (is_td (kvm)) -return -EOPNOTSUPP ;/* Not ready to create guest TD yet. */ +return tdx_vm_init (kvm);return vmx_vm_init (kvm); } +static void vt_flush_shadow_all_private (struct kvm *kvm) +{ +if (is_td (kvm)) +tdx_mmu_release_hkid (kvm); +} + +static void vt_vm_destroy (struct kvm *kvm) +{ +if (is_td (kvm)) +return ; + +vmx_vm_destroy (kvm); +} + +static void vt_vm_free (struct kvm *kvm) +{ +if (is_td (kvm)) +tdx_vm_free (kvm); +} +static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))@@ -93,7 +120,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .check_processor_compatibility = vmx_check_processor_compat, - .hardware_unsetup = vmx_hardware_unsetup, + .hardware_unsetup = vt_hardware_unsetup, .hardware_enable = vt_hardware_enable, .hardware_disable = vmx_hardware_disable,@@ -104,7 +131,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vm_size = sizeof(struct kvm_vmx), .vm_enable_cap = vt_vm_enable_cap, .vm_init = vt_vm_init, - .vm_destroy = vmx_vm_destroy, + .flush_shadow_all_private = vt_flush_shadow_all_private, + .vm_destroy = vt_vm_destroy, + .vm_free = vt_vm_free, .vcpu_precreate = vmx_vcpu_precreate, .vcpu_create = vmx_vcpu_create,arch/x86/kvm/vmx/tdx.c +147
-1@@ -5,9 +5,10 @@#include "capabilities.h" #include "x86_ops.h" -#include "x86.h" #include "mmu.h" #include "tdx.h" +#include "tdx_ops.h" +#include "x86.h" #undef pr_fmt#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt@@ -47,6 +48,289 @@ int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)return r; } +struct tdx_info { +u8 nr_tdcs_pages; +}; + +/* Info about the TDX module. */ +static struct tdx_info tdx_info__ro_after_init ; + +/* +* Some TDX SEAMCALLs (TDH.MNG.CREATE, TDH.PHYMEM.CACHE.WB, +* TDH.MNG.KEY.RECLAIMID, TDH.MNG.KEY.FREEID etc) tries to acquire a global lock +* internally in TDX module. If failed, TDX_OPERAND_BUSY is returned without +* spinning or waiting due to a constraint on execution time. It's caller's +* responsibility to avoid race (or retry on TDX_OPERAND_BUSY). Use this mutex +* to avoid race in TDX module because the kernel knows better about scheduling. +*/ +static DEFINE_MUTEX (tdx_lock); +static struct mutex *tdx_mng_key_config_lock; + +static __always_inline hpa_t set_hkid_to_hpa(hpa_t pa, u16 hkid) +{ +return pa | ((hpa_t )hkid << boot_cpu_data.x86_phys_bits); +} + +static inline bool is_td_created (struct kvm_tdx *kvm_tdx) +{ +return kvm_tdx->tdr_pa; +} + +static inline void tdx_hkid_free (struct kvm_tdx *kvm_tdx) +{ +tdx_guest_keyid_free (kvm_tdx->hkid); + kvm_tdx->hkid =-1 ; +} + +static inline bool is_hkid_assigned (struct kvm_tdx *kvm_tdx) +{ +return kvm_tdx->hkid >0 ; +} + +static void tdx_clear_page (unsigned long page_pa) +{ +const void *zero_page = (const void *)__va (page_to_phys (ZERO_PAGE (0 ))); +void *page =__va (page_pa); +unsigned long i; + +/* +* When re-assign one page from old keyid to a new keyid, MOVDIR64B is +* required to clear/write the page with new keyid to prevent integrity +* error when read on the page with new keyid. +* +* clflush doesn't flush cache with HKID set. The cache line could be +* poisoned (even without MKTME-i), clear the poison bit. +*/ +for (i =0 ; i <PAGE_SIZE ; i +=64 ) +movdir64b (page + i, zero_page); +/* +* MOVDIR64B store uses WC buffer. Prevent following memory reads +* from seeing potentially poisoned cache. +*/ +__mb (); +} + +static int __tdx_reclaim_page (hpa_t pa) +{ +struct tdx_module_args out; +u64 err; + +do { + err =tdh_phymem_page_reclaim (pa, &out); +/* +* TDH.PHYMEM.PAGE.RECLAIM is allowed only when TD is shutdown. +* state. i.e. destructing TD. +* TDH.PHYMEM.PAGE.RECLAIM requires TDR and target page. +* Because we're destructing TD, it's rare to contend with TDR. +*/ + }while (unlikely (err == (TDX_OPERAND_BUSY |TDX_OPERAND_ID_RCX ))); +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_PHYMEM_PAGE_RECLAIM , err, &out); +return -EIO ; + } + +return 0 ; +} + +static int tdx_reclaim_page (hpa_t pa) +{ +int r; + + r =__tdx_reclaim_page (pa); +if (!r) +tdx_clear_page (pa); +return r; +} + +static void tdx_reclaim_td_page (unsigned long td_page_pa) +{ +WARN_ON_ONCE (!td_page_pa); + +/* +* TDCX are being reclaimed. TDX module maps TDCX with HKID +* assigned to the TD. Here the cache associated to the TD +* was already flushed by TDH.PHYMEM.CACHE.WB before here, So +* cache doesn't need to be flushed again. +*/ +if (tdx_reclaim_page (td_page_pa)) +/* +* Leak the page on failure: +* tdx_reclaim_page() returns an error if and only if there's an +* unexpected, fatal error, e.g. a SEAMCALL with bad params, +* incorrect concurrency in KVM, a TDX Module bug, etc. +* Retrying at a later point is highly unlikely to be +* successful. +* No log here as tdx_reclaim_page() already did. +*/ +return ; +free_page ((unsigned long )__va (td_page_pa)); +} + +static void tdx_do_tdh_phymem_cache_wb (void *unused) +{ +u64 err =0 ; + +do { + err =tdh_phymem_cache_wb (!!err); + }while (err ==TDX_INTERRUPTED_RESUMABLE ); + +/* Other thread may have done for us. */ +if (err ==TDX_NO_HKID_READY_TO_WBCACHE ) + err =TDX_SUCCESS ; +if (WARN_ON_ONCE (err)) +pr_tdx_error (TDH_PHYMEM_CACHE_WB , err,NULL ); +} + +void tdx_mmu_release_hkid (struct kvm *kvm) +{ +bool packages_allocated, targets_allocated; +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +cpumask_var_t packages, targets; +u64 err; +int i; + +if (!is_hkid_assigned (kvm_tdx)) +return ; + +if (!is_td_created (kvm_tdx)) {
arch/x86/kvm/vmx/tdx.c int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 205
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+tdx_hkid_free (kvm_tdx); +return ; + } + + packages_allocated =zalloc_cpumask_var (&packages,GFP_KERNEL ); + targets_allocated =zalloc_cpumask_var (&targets,GFP_KERNEL ); +cpus_read_lock (); + +/* +* We can destroy multiple the guest TDs simultaneously. Prevent
I think there is an extra "the" here.
Quoted 15 lines
arch/x86/kvm/vmx/tdx.c +15
-0+* tdh_phymem_cache_wb from returning TDX_BUSY by serialization. +*/ +mutex_lock (&tdx_lock); + +/* +* Go through multiple TDX HKID state transitions with three SEAMCALLs +* to make TDH.PHYMEM.PAGE.RECLAIM() usable. Make the transition atomic +* to other functions to operate private pages and Secure-EPT pages. +* +* Avoid race for kvm_gmem_release() to call kvm_mmu_unmap_gfn_range(). +* This function is called via mmu notifier, mmu_release(). +* kvm_gmem_release() is called via fput() on process exit. +*/ +write_lock (&kvm->mmu_lock); +
arch/x86/kvm/vmx/tdx.c int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 230
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+for_each_online_cpu (i) { +if (packages_allocated && +cpumask_test_and_set_cpu (topology_physical_package_id (i), + packages)) +continue ; +if (targets_allocated) +cpumask_set_cpu (i, targets); + } +if (targets_allocated) +on_each_cpu_mask (targets, tdx_do_tdh_phymem_cache_wb,NULL ,1 );
Although in exist kernel code, there are a lot of such usages, but for new code, is it better to use 'true' for bool type ?
arch/x86/kvm/vmx/tdx.c int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 232
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +2
-0+else +on_each_cpu (tdx_do_tdh_phymem_cache_wb,NULL ,1 );
ditto
arch/x86/kvm/vmx/tdx.c int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 237
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +5
-0+/* +* In the case of error in tdx_do_tdh_phymem_cache_wb(), the following +* tdh_mng_key_freeid() will fail. +*/ +
An extra white line?
Quoted 15 lines
arch/x86/kvm/vmx/tdx.c +15
-0+ err =tdh_mng_key_freeid (kvm_tdx->tdr_pa); +if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MNG_KEY_FREEID , err,NULL ); +pr_err ("tdh_mng_key_freeid() failed. HKID %d is leaked.\n" , + kvm_tdx->hkid); + }else +tdx_hkid_free (kvm_tdx); + +write_unlock (&kvm->mmu_lock); +mutex_unlock (&tdx_lock); +cpus_read_unlock (); +free_cpumask_var (targets); +free_cpumask_var (packages); +} +
[...]
Re: [PATCH v17 021/116] KVM: TDX: initialize VM with TDX specific parameters
In reply to [email protected]Quoted 70 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX requires additional parameters for TDX VM for confidential execution to protect the confidentiality of its memory contents and CPU state from any other software, including VMM. When creating a guest TD VM before creating vcpu, the number of vcpu, TSC frequency (the values are the same among vcpus, and it can't change.) CPUIDs which the TDX module emulates. Guest TDs can trust those CPUIDs and sha384 values for measurement.
Add a new subcommand,
KVM_TDX_INIT_VM, to pass parameters for the TDX guest. It assigns an encryption key to the TDX guest for memory encryption. TDX encrypts memory per guest basis. The device model, say qemu, passes per-VM parameters for the TDX guest. The maximum number of vcpus, TSC frequency (TDX guest has fixed VM-wide TSC frequency, not per vcpu. The TDX guest can not change it.), attributes (production or debug), available extended features (which configure guest XCR0,IA32_XSSMSR), CPUIDs, sha384 measurements, etc.Call this subcommand before creating vcpu and
KVM_SET_CPUID2, i.e. CPUID configurations aren't available yet. So CPUIDs configuration values need to be passed in structkvm_tdx_init_vm. The device model's responsibility to make this CPUID config forKVM_TDX_INIT_VMandKVM_SET_CPUID2.Signed-off-by Xiaoyao Li <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>---
v15 -> v16:
- Removed AMX check as the KVM upstream supports AMX.
- Added CET flag to guest supported xssv14 -> v15:
- add check if the reserved area ofinit_vmis zero--- arch/x86/include/asm/tdx.h | 2 + arch/x86/include/uapi/asm/kvm.h | 27 +++ arch/x86/kvm/cpuid.c | 7 + arch/x86/kvm/cpuid.h | 2 + arch/x86/kvm/vmx/tdx.c | 263 +++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 18 ++ arch/x86/kvm/vmx/tdx_arch.h | 6 + tools/arch/x86/include/uapi/asm/kvm.h | 33 ++++ 8 files changed, 348 insertions(+), 10 deletions(-)arch/x86/include/asm/tdx.h +2
-0@@ -109,6 +109,8 @@ static inline u64 sc_retry(sc_func_t func, u64 fn,#define seamcall_ret (_fn, _args) sc_retry(__seamcall_ret, (_fn), (_args))#define seamcall_saved_ret (_fn, _args) sc_retry(__seamcall_saved_ret, (_fn), (_args)) +/* -1 indicates CPUID leaf with no sub-leaves. */ +#define TDX_CPUID_NO_SUBLEAF ((u32)-1)struct tdx_cpuid_config { __struct_group(tdx_cpuid_config_leaf, leaf_sub_leaf, __packed, u32 leaf;arch/x86/include/uapi/asm/kvm.h +1
-0@@ -570,6 +570,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 625
Quoted 8 lines
arch/x86/include/uapi/asm/kvm.h +5
-0@@ -617,4 +618,30 @@ struct kvm_tdx_capabilities {struct kvm_tdx_cpuid_config cpuid_configs[]; }; +struct kvm_tdx_init_vm { +__u64 attributes; +__u64 mrconfigid[6 ];/* sha384 digest */ +__u64 mrowner[6 ];/* sha384 digest */ +__u64 mrownerconfig[6 ];/* sha348 digest */
s/sha348/sha384
Also, the "struct kvm_tdx_init_vm" here is not aligned of which in tools/arch/x86/include/uapi/asm/kvm.h, which one is correct?
Quoted 173 lines
arch/x86/include/uapi/asm/kvm.h +21
-0+/* +* For future extensibility to make sizeof(struct kvm_tdx_init_vm) = 8KB. +* This should be enough given sizeof(TD_PARAMS) = 1024. +* 8KB was chosen given because +* sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES(=256) = 8KB. +*/ +__u64 reserved[1004 ]; + +/* +* Call KVM_TDX_INIT_VM before vcpu creation, thus before +* KVM_SET_CPUID2. +* This configuration supersedes KVM_SET_CPUID2s for VCPUs because the +* TDX module directly virtualizes those CPUIDs without VMM. The user +* space VMM, e.g. qemu, should make KVM_SET_CPUID2 consistent with +* those values. If it doesn't, KVM may have wrong idea of vCPUIDs of +* the guest, and KVM may wrongly emulate CPUIDs or MSRs that the TDX +* module doesn't virtualize. +*/ + struct kvm_cpuid2 cpuid; +}; +#endif /* _ASM_X86_KVM_H */ arch/x86/kvm/cpuid.c +7
-0@@ -1422,6 +1422,13 @@ int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,return r; } +struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2 ( +struct kvm_cpuid_entry2 *entries,int nent,u32 function,u64 index) +{ +return cpuid_entry2_find (entries, nent, function, index); +} +EXPORT_SYMBOL_GPL (kvm_find_cpuid_entry2); +struct kvm_cpuid_entry2 *kvm_find_cpuid_entry_index (struct kvm_vcpu *vcpu,u32 function,u32 index) {arch/x86/kvm/cpuid.h +2
-0@@ -13,6 +13,8 @@ void kvm_set_cpu_caps(void);void kvm_update_cpuid_runtime (struct kvm_vcpu *vcpu);void kvm_update_pv_runtime (struct kvm_vcpu *vcpu); +struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2 (struct kvm_cpuid_entry2 *entries, +int nent,u32 function,u64 index);struct kvm_cpuid_entry2 *kvm_find_cpuid_entry_index (struct kvm_vcpu *vcpu,u32 function,u32 index); struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,arch/x86/kvm/vmx/tdx.c +84
-6@@ -7,7 +7,6 @@#include "x86_ops.h" #include "mmu.h" #include "tdx.h" -#include "tdx_ops.h" #include "x86.h" #undef pr_fmt@@ -317,18 +316,21 @@ static int tdx_do_tdh_mng_key_config(void *param)return 0 ; } -static int __tdx_td_init (struct kvm *kvm); -int tdx_vm_init (struct kvm *kvm) { +/* +* This function initializes only KVM software construct. It doesn't +* initialize TDX stuff, e.g. TDCS, TDR, TDCX, HKID etc. +* It is handled by KVM_TDX_INIT_VM, __tdx_td_init(). +*/ +/* * TDX has its own limit of the number of vcpus in addition to * KVM_MAX_VCPUS. */ kvm->max_vcpus =min (kvm->max_vcpus,TDX_MAX_VCPUS ); -/* Place holder for TDX specific logic. */ -return __tdx_td_init (kvm); +return 0 ; }static int tdx_get_capabilities (struct kvm_tdx_cmd *cmd)@@ -391,9 +393,163 @@ static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd)return ret; } -static int __tdx_td_init (struct kvm *kvm) +static int setup_tdparams_eptp_controls (struct kvm_cpuid2 *cpuid, +struct td_params *td_params) +{ +const struct kvm_cpuid_entry2 *entry; +int max_pa =36 ; + + entry =kvm_find_cpuid_entry2 (cpuid->entries, cpuid->nent,0x80000008 ,0 ); +if (entry) + max_pa = entry->eax &0xff ; + + td_params->eptp_controls =VMX_EPTP_MT_WB ; +/* +* No CPU supports 4-level && max_pa > 48. +* "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." +* cpu_has_vmx_ept_5levels() check is just in case. +*/ +if (!cpu_has_vmx_ept_5levels () && max_pa >48 ) +return -EINVAL ; +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 ; + } + +return 0 ; +} + +static void setup_tdparams_cpuids (const struct tdsysinfo_struct *tdsysinfo, +struct kvm_cpuid2 *cpuid, +struct td_params *td_params) +{ +int i; + +/* +* td_params.cpuid_values: The number and the order of cpuid_value must +* be same to the one of struct tdsysinfo.{num_cpuid_config, cpuid_configs} +* It's assumed that td_params was zeroed. +*/ +for (i =0 ; i < tdsysinfo->num_cpuid_config; i++) { +const struct tdx_cpuid_config *config = &tdsysinfo->cpuid_configs[i]; +/* TDX_CPUID_NO_SUBLEAF in TDX CPUID_CONFIG means index = 0. */ +u32 index = config->sub_leaf ==TDX_CPUID_NO_SUBLEAF ?0 : config->sub_leaf; +const struct kvm_cpuid_entry2 *entry = +kvm_find_cpuid_entry2 (cpuid->entries, cpuid->nent, + config->leaf, index); +struct tdx_cpuid_value *value = &td_params->cpuid_values[i]; + +if (!entry) +continue ; + +/* +* tdsysinfo.cpuid_configs[].{eax, ebx, ecx, edx} +* bit 1 means it can be configured to zero or one. +* bit 0 means it must be zero. +* Mask out non-configurable bits. +*/ + value->eax = entry->eax & config->eax; + value->ebx = entry->ebx & config->ebx; + value->ecx = entry->ecx & config->ecx; + value->edx = entry->edx & config->edx; + } +} + +static int setup_tdparams_xfam (struct kvm_cpuid2 *cpuid,struct td_params *td_params) +{ +const struct kvm_cpuid_entry2 *entry; +u64 guest_supported_xcr0; +u64 guest_supported_xss; + +/* Setup td_params.xfam */ + entry =kvm_find_cpuid_entry2 (cpuid->entries, cpuid->nent,0xd ,0 ); +if (entry) + guest_supported_xcr0 = (entry->eax | ((u64 )entry->edx <<32 )); +else
arch/x86/kvm/vmx/tdx.c static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd) · line 482
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+ guest_supported_xcr0 =0 ; + guest_supported_xcr0 &= kvm_caps.supported_xcr0; + + entry =kvm_find_cpuid_entry2 (cpuid->entries, cpuid->nent,0xd ,1 ); +if (entry) + guest_supported_xss = (entry->ecx | ((u64 )entry->edx <<32 )); +else + guest_supported_xss =0 ; + +/* PT can be exposed to TD guest regardless of KVM's XSS support */
May also add description of CET to the comment.
Quoted 21 lines
arch/x86/kvm/vmx/tdx.c +21
-0+ guest_supported_xss &= + (kvm_caps.supported_xss |XFEATURE_MASK_PT |TDX_TD_XFAM_CET ); + + td_params->xfam = guest_supported_xcr0 | guest_supported_xss; +if (td_params->xfam &XFEATURE_MASK_LBR ) { +/* +* TODO: once KVM supports LBR(save/restore LBR related +* registers around TDENTER), remove this guard. +*/ +#define MSG_LBR "TD doesn't support LBR yet. KVM needs to save/restore IA32_LBR_DEPTH properly.\n" +pr_warn (MSG_LBR ); +return -EOPNOTSUPP ; + } + +return 0 ; +} + +static int setup_tdparams (struct kvm *kvm,struct td_params *td_params, +struct kvm_tdx_init_vm *init_vm) +{ +struct kvm_cpuid2 *cpuid = &init_vm->cpuid;
arch/x86/kvm/vmx/tdx.c static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd) · line 513
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+const struct tdsysinfo_struct *tdsysinfo; +int ret; + + tdsysinfo =tdx_get_sysinfo (); +if (!tdsysinfo) +return -EOPNOTSUPP ; +if (kvm->created_vcpus) +return -EBUSY ; + +if (td_params->attributes &TDX_TD_ATTRIBUTE_PERFMON ) {
It should check against init_vm->attributes,td_params->attributes is empty currently.
tools/arch/x86/include/uapi/asm/kvm.h
Quoted 220 lines
arch/x86/kvm/vmx/tdx.c +128
-4+/* +* TODO: save/restore PMU related registers around TDENTER. +* Once it's done, remove this guard. +*/ +#define MSG_PERFMON "TD doesn't support perfmon yet. KVM needs to save/restore host perf registers properly.\n" +pr_warn (MSG_PERFMON ); +return -EOPNOTSUPP ; + } + + td_params->max_vcpus = kvm->max_vcpus; + td_params->attributes = init_vm->attributes; + td_params->tsc_frequency =TDX_TSC_KHZ_TO_25MHZ (kvm->arch.default_tsc_khz); + + ret =setup_tdparams_eptp_controls (cpuid, td_params); +if (ret) +return ret; +setup_tdparams_cpuids (tdsysinfo, cpuid, td_params); + ret =setup_tdparams_xfam (cpuid, td_params); +if (ret) +return ret; + +#define MEMCPY_SAME_SIZE (dst, src) \ + do { \ + BUILD_BUG_ON(sizeof(dst) != sizeof(src)); \ + memcpy((dst), (src), sizeof(dst)); \ + } while (0) + +MEMCPY_SAME_SIZE (td_params->mrconfigid, init_vm->mrconfigid); +MEMCPY_SAME_SIZE (td_params->mrowner, init_vm->mrowner); +MEMCPY_SAME_SIZE (td_params->mrownerconfig, init_vm->mrownerconfig); + +return 0 ; +} + +static int __tdx_td_init (struct kvm *kvm,struct td_params *td_params, +u64 *seamcall_err) {struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +struct tdx_module_args out;cpumask_var_t packages;unsigned long *tdcs_pa =NULL ;unsigned long tdr_pa =0 ;@@ -401,6 +557,7 @@ static int __tdx_td_init(struct kvm *kvm)int ret, i;u64 err; + *seamcall_err =0 ; ret =tdx_guest_keyid_alloc ();if (ret <0 )return ret;@@ -506,10 +663,23 @@ static int __tdx_td_init(struct kvm *kvm) } } -/* -* Note, TDH_MNG_INIT cannot be invoked here. TDH_MNG_INIT requires a dedicated -* ioctl() to define the configure CPUID values for the TD. -*/ + err =tdh_mng_init (kvm_tdx->tdr_pa,__pa (td_params), &out); +if ((err &TDX_SEAMCALL_STATUS_MASK ) ==TDX_OPERAND_INVALID ) { +/* +* Because a user gives operands, don't warn. +* Return a hint to the user because it's sometimes hard for the +* user to figure out which operand is invalid. SEAMCALL status +* code includes which operand caused invalid operand error. +*/ + *seamcall_err = err; + ret = -EINVAL ; + goto teardown; + }else if (WARN_ON_ONCE (err)) { +pr_tdx_error (TDH_MNG_INIT , err, &out); + ret = -EIO ; + goto teardown; + } +return 0 ;/* @@ -552,6 +722,76 @@ static int __tdx_td_init(struct kvm *kvm)return ret; } +static int tdx_td_init (struct kvm *kvm,struct kvm_tdx_cmd *cmd) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +struct kvm_tdx_init_vm *init_vm =NULL ; +struct td_params *td_params =NULL ; +int ret; + +BUILD_BUG_ON (sizeof (*init_vm) !=8 *1024 ); +BUILD_BUG_ON (sizeof (struct td_params ) !=1024 ); + +if (is_hkid_assigned (kvm_tdx)) +return -EINVAL ; + +if (cmd->flags) +return -EINVAL ; + + init_vm =kzalloc (sizeof (*init_vm) + +sizeof (init_vm->cpuid.entries[0 ]) *KVM_MAX_CPUID_ENTRIES , +GFP_KERNEL ); +if (!init_vm) +return -ENOMEM ; +if (copy_from_user (init_vm, (void __user *)cmd->data,sizeof (*init_vm))) { + ret = -EFAULT ; + goto out; + } +if (init_vm->cpuid.nent >KVM_MAX_CPUID_ENTRIES ) { + ret = -E2BIG ; + goto out; + } +if (copy_from_user (init_vm->cpuid.entries, + (void __user *)cmd->data +sizeof (*init_vm), +flex_array_size (init_vm, cpuid.entries, init_vm->cpuid.nent))) { + ret = -EFAULT ; + goto out; + } + +if (memchr_inv (init_vm->reserved,0 ,sizeof (init_vm->reserved))) { + ret = -EINVAL ; + goto out; + } +if (init_vm->cpuid.padding) { + ret = -EINVAL ; + goto out; + } + + td_params =kzalloc (sizeof (struct td_params ),GFP_KERNEL ); +if (!td_params) { + ret = -ENOMEM ; + goto out; + } + + ret =setup_tdparams (kvm, td_params, init_vm); +if (ret) + goto out; + + ret =__tdx_td_init (kvm, td_params, &cmd->error); +if (ret) + goto out; + + kvm_tdx->tsc_offset =td_tdcs_exec_read64 (kvm_tdx,TD_TDCS_EXEC_TSC_OFFSET ); + kvm_tdx->attributes = td_params->attributes; + kvm_tdx->xfam = td_params->xfam; + +out: +/* kfree() accepts NULL. */ +kfree (init_vm); +kfree (td_params); +return ret; +} +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_tdx_cmd tdx_cmd;@@ -568,6 +808,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)case KVM_TDX_CAPABILITIES : r =tdx_get_capabilities (&tdx_cmd);break ; +case KVM_TDX_INIT_VM : + r =tdx_td_init (kvm, &tdx_cmd); +break ;default : r = -EINVAL ; goto out;arch/x86/kvm/vmx/tdx.h +18
-0@@ -12,7 +12,11 @@ struct kvm_tdx {unsigned long tdr_pa;unsigned long *tdcs_pa; +u64 attributes; +u64 xfam;int hkid; + +u64 tsc_offset; };struct vcpu_tdx {@@ -39,6 +43,20 @@ static inline struct vcpu_tdx *to_tdx(struct kvm_vcpu *vcpu) { return container_of(vcpu, struct vcpu_tdx, vcpu); } + +static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field) +{ + struct tdx_module_args out; + u64 err; + + err = tdh_mng_rd(kvm_tdx->tdr_pa, TDCS_EXEC(field), &out); + if (unlikely(err)) { + pr_err("TDH_MNG_RD[EXEC.0x%x] failed: 0x%llx\n", field, err); + return 0; + } + return out.r8; +} + #else struct kvm_tdx { struct kvm kvm;arch/x86/kvm/vmx/tdx_arch.h +6
-0@@ -122,6 +122,12 @@ struct tdx_cpuid_value {#define TDX_TD_ATTRIBUTE_KL BIT_ULL(31)#define TDX_TD_ATTRIBUTE_PERFMON BIT_ULL(63) +/* +* TODO: Once XFEATURE_CET_{U, S} in arch/x86/include/asm/fpu/types.h is +* defined, Replace these with define ones. +*/ +#define TDX_TD_XFAM_CET (BIT(11) | BIT(12)) +/* * TD_PARAMS is provided as an input to TDH_MNG_INIT, the size of which is 1024B. */ tools/arch/x86/include/uapi/asm/kvm.h +0
-0
Should drop this part according to
https://lore.kernel.org/all/Y8bZ%2FJ98V5i3wG%[email protected]/
Quoted 12 lines
index
7a08723e99e2..61ce7d174fcf100644tools/arch/x86/include/uapi/asm/kvm.h +1
-0@@ -565,6 +565,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 , };
tools/arch/x86/include/uapi/asm/kvm.h struct kvm_tdx_capabilities { · line 624
Quoted 10 lines
tools/arch/x86/include/uapi/asm/kvm.h +7
-0@@ -614,4 +615,36 @@ struct kvm_tdx_capabilities {struct kvm_tdx_cpuid_config cpuid_configs[]; }; +struct kvm_tdx_init_vm { +__u64 attributes; +__u32 max_vcpus; +__u32 padding; +__u64 mrconfigid[6 ];/* sha384 digest */ +__u64 mrowner[6 ];/* sha384 digest */ +__u64 mrownerconfig[6 ];/* sha348 digest */
s/sha348/sha384
Quoted 26 lines
tools/arch/x86/include/uapi/asm/kvm.h +25
-0+union { +/* +* KVM_TDX_INIT_VM is called before vcpu creation, thus before +* KVM_SET_CPUID2. CPUID configurations needs to be passed. +* +* This configuration supersedes KVM_SET_CPUID{,2}. +* The user space VMM, e.g. qemu, should make them consistent +* with this values. +* sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES(256) +* = 8KB. +*/ +struct { +struct kvm_cpuid2 cpuid; +/* 8KB with KVM_MAX_CPUID_ENTRIES. */ +struct kvm_cpuid_entry2 entries[]; + }; +/* +* For future extensibility. +* The size(struct kvm_tdx_init_vm) = 16KB. +* This should be enough given sizeof(TD_PARAMS) = 1024 +*/ +__u64 reserved[2028 ]; + }; +}; +#endif /* _ASM_X86_KVM_H */
Re: [PATCH v17 023/116] KVM: TDX: Refuse to unplug the last cpu on the package
In reply to [email protected]Quoted 11 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
In order to reclaim TDX HKID, (i.e. when deleting guest TD), needs to call TDH.PHYMEM.PAGE.WBINVD on all packages. If we have active TDX HKID, refuse to offline the last online cpu to guarantee at least one CPU online per package. Add arch callback for cpu offline.
Because TDX doesn't support suspend, this also refuses suspend if TDs are running. If no TD is running, suspend is allowed.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>
Quoted 193 lines
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/main.c | 1 + arch/x86/kvm/vmx/tdx.c | 41 ++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 2 ++ arch/x86/kvm/x86.c | 5 ++++ include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 12 +++++++-- 8 files changed, 62 insertions(+), 2 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -18,6 +18,7 @@ KVM_X86_OP(check_processor_compatibility) KVM_X86_OP(hardware_enable) KVM_X86_OP(hardware_disable) KVM_X86_OP(hardware_unsetup) +KVM_X86_OP_OPTIONAL_RET0(offline_cpu) KVM_X86_OP(has_emulated_msr) KVM_X86_OP(vcpu_after_set_cpuid) KVM_X86_OP(is_vm_type_supported)arch/x86/include/asm/kvm_host.h +1
-0@@ -1570,6 +1570,7 @@ struct kvm_x86_ops {int (*hardware_enable)(void );void (*hardware_disable)(void );void (*hardware_unsetup)(void ); +int (*offline_cpu)(void );bool (*has_emulated_msr)(struct kvm *kvm,u32 index);void (*vcpu_after_set_cpuid)(struct kvm_vcpu *vcpu);arch/x86/kvm/vmx/main.c +1
-0@@ -121,6 +121,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .check_processor_compatibility = vmx_check_processor_compat, .hardware_unsetup = vt_hardware_unsetup, + .offline_cpu = tdx_offline_cpu, .hardware_enable = vt_hardware_enable, .hardware_disable = vmx_hardware_disable,arch/x86/kvm/vmx/tdx.c +41
-0@@ -64,6 +64,7 @@ static struct tdx_info tdx_info __ro_after_init;*/ static DEFINE_MUTEX (tdx_lock);static struct mutex *tdx_mng_key_config_lock; +static atomic_t nr_configured_hkid; static __always_inline hpa_t set_hkid_to_hpa(hpa_t pa, u16 hkid) {@@ -79,6 +80,7 @@ static inline void tdx_hkid_free(struct kvm_tdx *kvm_tdx) {tdx_guest_keyid_free (kvm_tdx->hkid); kvm_tdx->hkid =-1 ; +atomic_dec (&nr_configured_hkid); }static inline bool is_hkid_assigned (struct kvm_tdx *kvm_tdx)@@ -562,6 +564,7 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,if (ret <0 )return ret; kvm_tdx->hkid = ret; +atomic_inc (&nr_configured_hkid); va =__get_free_page (GFP_KERNEL_ACCOUNT );if (!va)@@ -932,3 +935,41 @@ void tdx_hardware_unsetup(void)/* kfree accepts NULL. */ kfree (tdx_mng_key_config_lock); } + +int tdx_offline_cpu (void ) +{ +int curr_cpu =smp_processor_id (); +cpumask_var_t packages; +int ret =0 ; +int i; + +/* No TD is running. Allow any cpu to be offline. */ +if (!atomic_read (&nr_configured_hkid)) +return 0 ; + +/* +* In order to reclaim TDX HKID, (i.e. when deleting guest TD), need to +* call TDH.PHYMEM.PAGE.WBINVD on all packages to program all memory +* controller with pconfig. If we have active TDX HKID, refuse to +* offline the last online cpu. +*/ +if (!zalloc_cpumask_var (&packages,GFP_KERNEL )) +return -ENOMEM ; +for_each_online_cpu (i) { +if (i != curr_cpu) +cpumask_set_cpu (topology_physical_package_id (i), packages); + } +/* Check if this cpu is the last online cpu of this package. */ +if (!cpumask_test_cpu (topology_physical_package_id (curr_cpu), packages)) + ret = -EBUSY ; +free_cpumask_var (packages); +if (ret) +/* +* Because it's hard for human operator to understand the +* reason, warn it. +*/ +#define MSG_ALLPKG_ONLINE \ +"TDX requires all packages to have an online CPU. Delete all TDs in order to offline all CPUs of a package.\n" + pr_warn_ratelimited(MSG_ALLPKG_ONLINE ); +return ret; +}arch/x86/kvm/vmx/x86_ops.h +2
-0@@ -138,6 +138,7 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops);void tdx_hardware_unsetup (void );bool tdx_is_vm_type_supported (unsigned long type); +int tdx_offline_cpu (void );int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap);int tdx_vm_init (struct kvm *kvm);@@ -148,6 +149,7 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp);static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {}static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; } +static inline int tdx_offline_cpu (void ) {return 0 ; }static inline int tdx_vm_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) {arch/x86/kvm/x86.c +5
-0@@ -12421,6 +12421,11 @@ void kvm_arch_hardware_disable(void)drop_user_return_notifiers (); } +int kvm_arch_offline_cpu (unsigned int cpu) +{ +return static_call (kvm_x86_offline_cpu)(); +} +bool kvm_vcpu_is_reset_bsp (struct kvm_vcpu *vcpu) {return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;include/linux/kvm_host.h +1
-0@@ -1502,6 +1502,7 @@ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {}int kvm_arch_hardware_enable (void );void kvm_arch_hardware_disable (void );#endif +int kvm_arch_offline_cpu (unsigned int cpu);int kvm_arch_vcpu_runnable (struct kvm_vcpu *vcpu);bool kvm_arch_vcpu_in_kernel (struct kvm_vcpu *vcpu);int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu);virt/kvm/kvm_main.c +10
-2@@ -5578,13 +5578,21 @@ static void hardware_disable_nolock(void *junk)__this_cpu_write (hardware_enabled, false); } +__weak int kvm_arch_offline_cpu (unsigned int cpu) +{ +return 0 ; +} +static int kvm_offline_cpu (unsigned int cpu) { +int r =0 ; +mutex_lock (&kvm_lock); -if (kvm_usage_count) + r =kvm_arch_offline_cpu (cpu); +if (!r && kvm_usage_count)hardware_disable_nolock (NULL );mutex_unlock (&kvm_lock); -return 0 ; +return r; }static void hardware_disable_all_nolock (void )
Re: [PATCH v17 025/116] KVM: TDX: allocate/free TDX vcpu structure
In reply to [email protected]Quoted 4 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
The next step of TDX guest creation is to create vcpu. Allocate TDX vcpu structures, initialize it that doesn't require TDX SEAMCALL.
Didn't see any allocation happens for TDX in this patch.
Quoted 39 lines
TDX specific
vcpu initialization will be implemented as independentKVM_TDX_INIT_VCPUso that when error occurs it's easy to determine which component has the issue, KVM or TDX.Signed-off-by Isaku Yamahata <[email protected]>---
v15 -> v16:
- Add AMX support as the KVM upstream supports it.--- arch/x86/kvm/vmx/main.c | 44 ++++++++++++++++++++++++++++++---- arch/x86/kvm/vmx/tdx.c | 49 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/x86_ops.h | 10 ++++++++ arch/x86/kvm/x86.c | 2 ++ 4 files changed, 101 insertions(+), 4 deletions(-)arch/x86/kvm/vmx/main.c +15
-0@@ -98,6 +98,42 @@ static void vt_vm_free(struct kvm *kvm)tdx_vm_free (kvm); } +static int vt_vcpu_precreate (struct kvm *kvm) +{ +if (is_td (kvm)) +return 0 ; + +return vmx_vcpu_precreate (kvm); +} + +static int vt_vcpu_create (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) +return tdx_vcpu_create (vcpu); + +return vmx_vcpu_create (vcpu); +}
arch/x86/kvm/vmx/main.c static void vt_vm_free(struct kvm *kvm) · line 125
Quoted 10 lines
arch/x86/kvm/vmx/main.c +10
-0+ +static void vt_vcpu_free (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_vcpu_free (vcpu); +return ; + } + +vmx_vcpu_free (vcpu); +}
I'm wondering for such wrapper functions without a return value, is the following style cleaner?
It can save two lines of code.
+static void vt_vcpu_free(struct kvm_vcpu *vcpu)
+{
+ if (is_td_vcpu(vcpu))
+ tdx_vcpu_free(vcpu);
+ else
+ vmx_vcpu_free(vcpu);
+}
arch/x86/kvm/vmx/main.c static void vt_vm_free(struct kvm *kvm) · line 136
Quoted 11 lines
arch/x86/kvm/vmx/main.c +11
-0+ +static void vt_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) +{ +if (is_td_vcpu (vcpu)) { +tdx_vcpu_reset (vcpu, init_event); +return ; + } + +vmx_vcpu_reset (vcpu, init_event); +} +
ditto
Quoted 128 lines
arch/x86/kvm/vmx/main.c +4
-4static int vt_mem_enc_ioctl (struct kvm *kvm,void __user *argp) {if (!is_td (kvm))@@ -136,10 +172,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vm_destroy = vt_vm_destroy, .vm_free = vt_vm_free, - .vcpu_precreate = vmx_vcpu_precreate, - .vcpu_create = vmx_vcpu_create, - .vcpu_free = vmx_vcpu_free, - .vcpu_reset = vmx_vcpu_reset, + .vcpu_precreate = vt_vcpu_precreate, + .vcpu_create = vt_vcpu_create, + .vcpu_free = vt_vcpu_free, + .vcpu_reset = vt_vcpu_reset, .prepare_switch_to_guest = vmx_prepare_switch_to_guest, .vcpu_load = vmx_vcpu_load,arch/x86/kvm/vmx/tdx.c +49
-0@@ -335,6 +335,55 @@ int tdx_vm_init(struct kvm *kvm)return 0 ; } +int tdx_vcpu_create (struct kvm_vcpu *vcpu) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (vcpu->kvm); + +/* +* On cpu creation, cpuid entry is blank. Forcibly enable +* X2APIC feature to allow X2APIC. +* Because vcpu_reset() can't return error, allocation is done here. +*/ +WARN_ON_ONCE (vcpu->arch.cpuid_entries); +WARN_ON_ONCE (vcpu->arch.cpuid_nent); + +/* TDX only supports x2APIC, which requires an in-kernel local APIC. */ +if (!vcpu->arch.apic) +return -EINVAL ; + +fpstate_set_confidential (&vcpu->arch.guest_fpu); + + vcpu->arch.efer =EFER_SCE |EFER_LME |EFER_LMA |EFER_NX ; + + vcpu->arch.cr0_guest_owned_bits =-1ul ; + vcpu->arch.cr4_guest_owned_bits =-1ul ; + + vcpu->arch.tsc_offset =to_kvm_tdx (vcpu->kvm)->tsc_offset; + vcpu->arch.l1_tsc_offset = vcpu->arch.tsc_offset; + vcpu->arch.guest_state_protected = + !(to_kvm_tdx (vcpu->kvm)->attributes &TDX_TD_ATTRIBUTE_DEBUG ); + +if ((kvm_tdx->xfam &XFEATURE_MASK_XTILE ) ==XFEATURE_MASK_XTILE ) + vcpu->arch.xfd_no_write_intercept = true; + +return 0 ; +} + +void tdx_vcpu_free (struct kvm_vcpu *vcpu) +{ +/* This is stub for now. More logic will come. */ +} + +void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event) +{ + +/* Ignore INIT silently because TDX doesn't support INIT event. */ +if (init_event) +return ; + +/* This is stub for now. More logic will come here. */ +} +static int tdx_get_capabilities (struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;arch/x86/kvm/vmx/x86_ops.h +10
-0@@ -144,7 +144,12 @@ int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap);int tdx_vm_init (struct kvm *kvm);void tdx_mmu_release_hkid (struct kvm *kvm);void tdx_vm_free (struct kvm *kvm); +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp); + +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);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }static inline void tdx_hardware_unsetup (void ) {}@@ -158,7 +163,12 @@ static inline int tdx_vm_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)static inline int tdx_vm_init (struct kvm *kvm) {return -EOPNOTSUPP ; }static inline void tdx_mmu_release_hkid (struct kvm *kvm) {}static inline void tdx_vm_free (struct kvm *kvm) {} +static inline int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {return -EOPNOTSUPP ; } + +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) {}#endif #endif /* __KVM_X86_VMX_X86_OPS_H */ arch/x86/kvm/x86.c +2
-0@@ -502,6 +502,7 @@ int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)kvm_recalculate_apic_map (vcpu->kvm);return 0 ; } +EXPORT_SYMBOL_GPL (kvm_set_apic_base);/* * Handle a fault on a hardware virtualization (VMX or SVM) instruction. @@ -12430,6 +12431,7 @@ bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) {return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; } +EXPORT_SYMBOL_GPL (kvm_vcpu_is_reset_bsp);bool kvm_vcpu_is_bsp (struct kvm_vcpu *vcpu) {
Re: [PATCH v17 029/116] KVM: x86/mmu: Add address conversion functions for TDX shared bit of GPA
In reply to [email protected]Quoted 12 lines · On 11/7/2023 10:55 PM, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
TDX repurposes one GPA bit (51 bit or 47 bit based on configuration) to indicate the GPA is private(if cleared) or shared (if set) with VMM. If GPA.shared is set, GPA is covered by the existing conventional EPT pointed by EPTP. If GPA.shared bit is cleared, GPA is covered by TDX module. VMM has to issue SEAMCALLs to operate.
Add a member to remember GPA shared bit for each guest TDs, add address conversion functions between private GPA and shared GPA and test if GPA is private.
Quoted 8 lines
Because struct
kvm_arch(or struct kvm which includes structkvm_arch. Seekvm_arch_alloc_vm()that passes__GPF_ZERO) is zero-cleared when allocated, the new member to remember GPA shared bit is guaranteed to be zero with this patch unless it's initialized explicitly.Co-developed-by Rick Edgecombe <[email protected]>Signed-off-by Rick Edgecombe <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>
Quoted 73 lines
--- arch/x86/include/asm/kvm_host.h | 4 ++++ arch/x86/kvm/mmu.h | 27 +++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.c | 5 +++++ 3 files changed, 36 insertions(+)arch/x86/include/asm/kvm_host.h +4
-0@@ -1475,6 +1475,10 @@ struct kvm_arch {*/ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1)struct kvm_mmu_memory_cache split_desc_cache; + +#ifdef CONFIG_KVM_MMU_PRIVATE +gfn_t gfn_shared_mask; +#endif };struct kvm_vm_stat {arch/x86/kvm/mmu.h +27
-0@@ -311,4 +311,31 @@ static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu,return gpa; return translate_nested_gpa(vcpu, gpa, access, exception); } + +static inline gfn_t kvm_gfn_shared_mask (const struct kvm *kvm) +{ +#ifdef CONFIG_KVM_MMU_PRIVATE +return kvm->arch.gfn_shared_mask; +#else +return 0 ; +#endif +} + +static inline gfn_t kvm_gfn_to_shared (const struct kvm *kvm,gfn_t gfn) +{ +return gfn |kvm_gfn_shared_mask (kvm); +} + +static inline gfn_t kvm_gfn_to_private (const struct kvm *kvm,gfn_t gfn) +{ +return gfn & ~kvm_gfn_shared_mask (kvm); +} + +static inline bool kvm_is_private_gpa (const struct kvm *kvm,gpa_t gpa) +{ +gfn_t mask =kvm_gfn_shared_mask (kvm); + +return mask && !(gpa_to_gfn (gpa) & mask); +} +#endif arch/x86/kvm/vmx/tdx.c +5
-0@@ -878,6 +878,11 @@ static int tdx_td_init(struct kvm *kvm, struct kvm_tdx_cmd *cmd) kvm_tdx->attributes = td_params->attributes; kvm_tdx->xfam = td_params->xfam; +if (td_params->exec_controls &TDX_EXEC_CONTROL_MAX_GPAW ) + kvm->arch.gfn_shared_mask =gpa_to_gfn (BIT_ULL (51 )); +else + kvm->arch.gfn_shared_mask =gpa_to_gfn (BIT_ULL (47 )); + out:/* kfree() accepts NULL. */ kfree (init_vm);
Re: [PATCH v17 007/116] KVM: TDX: Make TDX VM type supported
In reply to [email protected]Quoted 54 lines · On Tue, Nov 7, 2023 at 6:57 AM <[email protected]> wrote:
From: Isaku Yamahata <[email protected]>
NOTE: This patch is in position of the patch series for developers to be able to test codes during the middle of the patch series although this patch series doesn't provide functional features until the all the patches of this patch series. When merging this patch series, this patch can be moved to the end.
As first step TDX VM support, return that TDX VM type supported to device model, e.g. qemu. The callback to create guest TD is
vm_initcallback forKVM_CREATE_VM.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/main.c | 18 ++++++++++++++++-- arch/x86/kvm/vmx/tdx.c | 6 ++++++ arch/x86/kvm/vmx/vmx.c | 6 ------ arch/x86/kvm/vmx/x86_ops.h | 3 ++- 4 files changed, 24 insertions(+), 9 deletions(-)arch/x86/kvm/vmx/main.c +14
-0@@ -10,6 +10,12 @@static bool enable_tdx__ro_after_init ;module_param_named (tdx, enable_tdx, bool,0444 ); +static bool vt_is_vm_type_supported (unsigned long type) +{ +return __kvm_is_vm_type_supported (type) || + (enable_tdx &&tdx_is_vm_type_supported (type)); +} +static int vt_hardware_enable (void ) {int ret;@@ -37,6 +43,14 @@ static __init int vt_hardware_setup(void)return 0 ; } +static int vt_vm_init (struct kvm *kvm) +{ +if (is_td (kvm)) +return -EOPNOTSUPP ;/* Not ready to create guest TD yet. */ + +return vmx_vm_init (kvm); +} +#define VMX_REQUIRED_APICV_INHIBITS \ (BIT(APICV_INHIBIT_REASON_DISABLE)| \ BIT(APICV_INHIBIT_REASON_ABSENT) | \
arch/x86/kvm/vmx/main.c struct kvm_x86_ops vt_x86_ops __initdata = { · line 74
Quoted 5 lines
arch/x86/kvm/vmx/main.c +1
-1@@ -57,9 +71,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .hardware_disable = vmx_hardware_disable, .has_emulated_msr = vmx_has_emulated_msr, - .is_vm_type_supported = vmx_is_vm_type_supported, + .is_vm_type_supported = vt_is_vm_type_supported,
I don't see is_vm_type_supported defined at HEAD.
I could only find it here:
https://lore.kernel.org/lkml/ab9d8654bd98ae24de05788a2ecaa4bea6c0c44b.1689893403.git.isaku.yamahata@intel.com/#r
Is [RFC PATCH v4 00/10] KVM: guest_memfd(), X86: Common base ..." a dependency for this patch series?
Quoted 66 lines
arch/x86/kvm/vmx/main.c +1
-1.vm_size = sizeof(struct kvm_vmx), - .vm_init = vmx_vm_init, + .vm_init = vt_vm_init, .vm_destroy = vmx_vm_destroy, .vcpu_precreate = vmx_vcpu_precreate,arch/x86/kvm/vmx/tdx.c +6
-0@@ -24,6 +24,12 @@ static int __init tdx_module_setup(void)return 0 ; } +bool tdx_is_vm_type_supported (unsigned long type) +{ +/* enable_tdx check is done by the caller. */ +return type ==KVM_X86_TDX_VM ; +} +struct vmx_tdx_enabled {cpumask_var_t vmx_enabled;atomic_t err;arch/x86/kvm/vmx/vmx.c +0
-6@@ -7532,12 +7532,6 @@ int vmx_vcpu_create(struct kvm_vcpu *vcpu)return err; } -bool vmx_is_vm_type_supported (unsigned long type) -{ -/* TODO: Check if TDX is supported. */ -return __kvm_is_vm_type_supported (type); -} -#define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"#define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"arch/x86/kvm/vmx/x86_ops.h +2
-2@@ -28,7 +28,6 @@ void vmx_hardware_unsetup(void);int vmx_check_processor_compat (void );int vmx_hardware_enable (void );void vmx_hardware_disable (void ); -bool vmx_is_vm_type_supported (unsigned long type);int vmx_vm_init (struct kvm *kvm);void vmx_vm_destroy (struct kvm *kvm);int vmx_vcpu_precreate (struct kvm *kvm);@@ -137,8 +136,10 @@ void vmx_setup_mce(struct kvm_vcpu *vcpu);#ifdef CONFIG_INTEL_TDX_HOST int __init tdx_hardware_setup (struct kvm_x86_ops *x86_ops); +bool tdx_is_vm_type_supported (unsigned long type);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; } +static inline bool tdx_is_vm_type_supported (unsigned long type) {return false; }#endif #endif /* __KVM_X86_VMX_X86_OPS_H */ --2.25.1
Re: [PATCH v17 011/116] KVM: TDX: Add C wrapper functions for SEAMCALLs to the TDX module
In reply to [email protected]Quoted 26 lines · On Tue, 2023-11-07 at 06:55 -0800, [email protected] wrote:
arch/x86/kvm/vmx/tdx_ops.h new file +26
-0+static inline u64 tdx_seamcall (u64 op,u64 rcx,u64 rdx,u64 r8,u64 r9, + struct tdx_module_args *out) +{ +u64 ret; + +if (out) { + *out = (struct tdx_module_args ) { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall_ret (op, out); + }else { +struct tdx_module_args args = { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall (op, &args); + } +if (unlikely (ret ==TDX_SEAMCALL_UD )) { +/* +* SEAMCALLs fail with TDX_SEAMCALL_UD returned when VMX is off. +* This can happen when the host gets rebooted or live
arch/x86/kvm/vmx/tdx_ops.h line 52
Quoted 10 lines
arch/x86/kvm/vmx/tdx_ops.h new file +10
-0+* updated. In this case, the instruction execution is ignored +* as KVM is shut down, so the error code is suppressed. Other +* than this, the error is unexpected and the execution can't +* continue as the TDX features reply on VMX to be on. +*/ +kvm_spurious_fault (); +return 0 ; + } +return ret; +}
Why is tdx_seamcall() still taking individual registers as input? This is not extendable for supporting SEAMCALLs taking more input registers such as live migration SEAMCALLs.
It's OK to take individual inputs for the SEAMCALL leaf wrappers like ...
arch/x86/kvm/vmx/tdx_ops.h line 58
Quoted 6 lines
arch/x86/kvm/vmx/tdx_ops.h new file +6
-0+ +static inline u64 tdh_mng_addcx (hpa_t tdr,hpa_t addr) +{ +clflush_cache_range (__va (addr),PAGE_SIZE ); +return tdx_seamcall (TDH_MNG_ADDCX , addr, tdr,0 ,0 ,NULL ); +}
... this, but the tdx_seamcall() should just take 'struct tdx_module_args' as argument.
Re: [PATCH v17 012/116] KVM: TDX: Retry SEAMCALL on the lack of entropy error
In reply to [email protected]Quoted 74 lines · On Tue, 2023-11-07 at 06:55 -0800, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Some SEAMCALL may return
TDX_RND_NO_ENTROPYerror when the entropy is lacking. Retry SEAMCALL on the error followingrdrand_long()to retryRDRAND_RETRY_LOOPStimes.Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx_errno.h | 1 + arch/x86/kvm/vmx/tdx_ops.h | 40 +++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 17 deletions(-)arch/x86/kvm/vmx/tdx_errno.h +1
-0@@ -14,6 +14,7 @@#define TDX_OPERAND_INVALID 0xC000010000000000ULL#define TDX_OPERAND_BUSY 0x8000020000000000ULL#define TDX_PREVIOUS_TLB_EPOCH_BUSY 0x8000020100000000ULL +#define TDX_RND_NO_ENTROPY 0x8000020300000000ULL#define TDX_VCPU_NOT_ASSOCIATED 0x8000070200000000ULL#define TDX_KEY_GENERATION_FAILED 0x8000080000000000ULL#define TDX_KEY_STATE_INCORRECT 0xC000081100000000ULLarch/x86/kvm/vmx/tdx_ops.h +16
-17@@ -6,6 +6,7 @@#include <linux/compiler.h> +#include <asm/archrandom.h> #include <asm/cacheflush.h> #include <asm/asm.h> #include <asm/kvm_host.h> @@ -17,25 +18,30 @@static inline u64 tdx_seamcall (u64 op,u64 rcx,u64 rdx,u64 r8,u64 r9,struct tdx_module_args *out) { +int retry;u64 ret; -if (out) { - *out = (struct tdx_module_args ) { - .rcx = rcx, - .rdx = rdx, - .r8 = r8, - .r9 = r9, - }; - ret =__seamcall_ret (op, out); - }else { -struct tdx_module_args args = { - .rcx = rcx, - .rdx = rdx, - .r8 = r8, - .r9 = r9, - }; - ret =__seamcall (op, &args); - } +/* Mimic the existing rdrand_long() to retry RDRAND_RETRY_LOOPS times. */ + retry =RDRAND_RETRY_LOOPS ; + do { + if (out) { + *out = (struct tdx_module_args) { + .rcx = rcx, + .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret = __seamcall_ret(op, out); + } else { + struct tdx_module_args args = { + .rcx = rcx,
arch/x86/kvm/vmx/tdx_ops.h line 47
Quoted 10 lines
arch/x86/kvm/vmx/tdx_ops.h +7
-0+ .rdx = rdx, + .r8 = r8, + .r9 = r9, + }; + ret =__seamcall (op, &args); + } + }while (unlikely (ret ==TDX_RND_NO_ENTROPY ) && --retry);if (unlikely (ret ==TDX_SEAMCALL_UD )) {/* * SEAMCALLs fail with TDX_SEAMCALL_UD returned when VMX is off.
Why we cannot use seamcall*() variants directly which already handles running out of entropy error?
Sean Christopherson
maintainerRe: [PATCH v17 092/116] KVM: TDX: Handle TDX PV HLT hypercall
In reply to [email protected]Quoted 33 lines · On Tue, Nov 07, 2023, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV HLT hypercall to the KVM backend function.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 42 +++++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 3 +++ 2 files changed, 44 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/tdx.c +14
-1@@ -662,7 +662,32 @@ void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)bool tdx_protected_apic_has_interrupt (struct kvm_vcpu *vcpu) { -return pi_has_pending_interrupt (vcpu); +bool ret =pi_has_pending_interrupt (vcpu); +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (ret || vcpu->arch.mp_state !=KVM_MP_STATE_HALTED ) +return true; + +if (tdx->interrupt_disabled_hlt) +return false; + +/* +* 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
arch/x86/kvm/vmx/tdx.c void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 688
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+* TDCALL completes before interrupts are enabled. +* +* TDX modules sets RVI while in an STI interrupt shadow. +* - TDExit(typically TDG.VP.VMCALL<HLT>) from the guest to TDX module. +* 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 TDExit nevertheless, to a clueless hypervisor that +* has no way to glean either RVI or PPR.
WTF. Seriously, what in the absolute hell is going on. I reported this internally four ***YEARS*** ago. This is not some obscure theoretical edge case, this is core functionality and it's completely broken garbage.
NAK. Hard NAK. Fix the TDX module, full stop.
Even worse, TDX 1.5 apparently _already_ has the necessary logic for dealing with interrupts that are pending in RVI when handling NESTED VM-Enter. Really!?!?! Y'all went and added nested virtualization support of some kind, but can't find the time to get the basics right?
Re: [PATCH v17 092/116] KVM: TDX: Handle TDX PV HLT hypercall
Quoted 49 lines · Sean Christopherson
Quoted 33 lines · On Tue, Nov 07, 2023, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV HLT hypercall to the KVM backend function.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 42 +++++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 3 +++ 2 files changed, 44 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/tdx.c +14
-1@@ -662,7 +662,32 @@ void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)bool tdx_protected_apic_has_interrupt (struct kvm_vcpu *vcpu) { -return pi_has_pending_interrupt (vcpu); +bool ret =pi_has_pending_interrupt (vcpu); +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (ret || vcpu->arch.mp_state !=KVM_MP_STATE_HALTED ) +return true; + +if (tdx->interrupt_disabled_hlt) +return false; + +/* +* 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 arch/x86/kvm/vmx/tdx.c void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 688
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+* TDCALL completes before interrupts are enabled. +* +* TDX modules sets RVI while in an STI interrupt shadow. +* - TDExit(typically TDG.VP.VMCALL<HLT>) from the guest to TDX module. +* 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 TDExit nevertheless, to a clueless hypervisor that +* has no way to glean either RVI or PPR. WTF. Seriously, what in the absolute hell is going on. I reported this internally four ***YEARS*** ago. This is not some obscure theoretical edge case, this is core functionality and it's completely broken garbage.
Quoted 6 lines
NAK. Hard NAK. Fix the TDX module, full stop.
Even worse, TDX 1.5 apparently _already_ has the necessary logic for dealing with interrupts that are pending in RVI when handling NESTED VM-Enter. Really!?!?! Y'all went and added nested virtualization support of some kind, but can't find the time to get the basics right?
We actually fixed the TDX module. See 11.9.5. Pending Virtual Interrupt Delivery Indication in TDX module 1.5 spec [1]
The host VMM can detect whether a virtual interrupt is pending delivery to a VCPU in the Virtual APIC page, using TDH.VP.RD to read the VCPU_STATE_DETAILS TDVPS field.
The typical use case is when the guest TD VCPU indicates to the host VMM, using TDG.VP.VMCALL, that it has no work to do and can be halted. The guest TD is expected to pass an “interrupt blocked” flag. The guest TD is expected to set this flag to 0 if and only if RFLAGS.IF is 1 or the TDCALL instruction that invokes TDG.VP.VMCALL immediately follows an STI instruction. If the “interrupt blocked” flag is 0, the host VMM can determine whether to re-schedule the guest TD VCPU based on VCPU_STATE_DETAILS.
Isaku, this patch didn't read VCPU_STATE_DETAILS. Maybe you missed something during rebase? Regarding buggy_hlt_workaround, do you aim to avoid reading VCPU_STATE_DETAILS as much as possible (because reading it via SEAMCALL is costly, ~3-4K cycles)? if so, please make it clear in the changelog/comment.
[1]: https://cdrdv2.intel.com/v1/dl/getContent/733575
Quoted 1 line
Sean Christopherson
maintainerRe: [PATCH v17 092/116] KVM: TDX: Handle TDX PV HLT hypercall
Quoted 72 lines · Chao Gao
Quoted 49 lines · Sean Christopherson
Quoted 33 lines · On Tue, Nov 07, 2023, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV HLT hypercall to the KVM backend function.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 42 +++++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 3 +++ 2 files changed, 44 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/tdx.c +14
-1@@ -662,7 +662,32 @@ void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)bool tdx_protected_apic_has_interrupt (struct kvm_vcpu *vcpu) { -return pi_has_pending_interrupt (vcpu); +bool ret =pi_has_pending_interrupt (vcpu); +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (ret || vcpu->arch.mp_state !=KVM_MP_STATE_HALTED ) +return true; + +if (tdx->interrupt_disabled_hlt) +return false; + +/* +* 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 arch/x86/kvm/vmx/tdx.c void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 688
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+* TDCALL completes before interrupts are enabled. +* +* TDX modules sets RVI while in an STI interrupt shadow. +* - TDExit(typically TDG.VP.VMCALL<HLT>) from the guest to TDX module. +* 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 TDExit nevertheless, to a clueless hypervisor that +* has no way to glean either RVI or PPR. WTF. Seriously, what in the absolute hell is going on. I reported this internally four ***YEARS*** ago. This is not some obscure theoretical edge case, this is core functionality and it's completely broken garbage.
Quoted 6 lines
NAK. Hard NAK. Fix the TDX module, full stop.
Even worse, TDX 1.5 apparently _already_ has the necessary logic for dealing with interrupts that are pending in RVI when handling NESTED VM-Enter. Really!?!?! Y'all went and added nested virtualization support of some kind, but can't find the time to get the basics right?
We actually fixed the TDX module. See 11.9.5. Pending Virtual Interrupt Delivery Indication in TDX module 1.5 spec [1]
The host VMM can detect whether a virtual interrupt is pending delivery to a VCPU in the Virtual APIC page, using TDH.VP.RD to read the
VCPU_STATE_DETAILSTDVPS field.The typical use case is when the guest TD VCPU indicates to the host VMM, using TDG.VP.VMCALL, that it has no work to do and can be halted. The guest TD is expected to pass an “interrupt blocked” flag. The guest TD is expected to set this flag to 0 if and only if RFLAGS.IF is 1 or the TDCALL instruction that invokes TDG.VP.VMCALL immediately follows an STI instruction. If the “interrupt blocked” flag is 0, the host VMM can determine whether to re-schedule the guest TD VCPU based on
VCPU_STATE_DETAILS.
Quoted 4 lines
Isaku, this patch didn't read
VCPU_STATE_DETAILS. Maybe you missed something during rebase? Regardingbuggy_hlt_workaround, do you aim to avoid readingVCPU_STATE_DETAILSas much as possible (because reading it via SEAMCALL is costly, ~3-4K cycles)?
*sigh* Why only earth doesn't the TDX module simply compute VMXIP on TDVMCALL? It's literally one bit and one extra VMREAD. There are plenty of register bits available, and I highly doubt ~20 cycles in the TDVMCALL path will be noticeable, let alone problematic. Such functionality could even be added on top in a TDX module update, and Intel could even bill it as a performance optimization.
Eating 4k cycles in the HLT path isn't the end of the world, but it's far from optimal and it's just so incredibly wasteful. I wouldn't be surprised if the latency is measurable for certain workloads, which will lead to guests using idle=poll and/or other games being played in the guest.
And AFAICT, the TDX module doesn't support HLT passthrough, so fully dedicated CPUs can't even mitigate the pain that way.
Anyways, regarding the "workaround", my NAK stands. It has bad tradeoffs of its own, e.g. will result in spurious wakeups, and can't possibly work for VMs with passthrough devices. Not to mention that the implementation has several races and false positives.
Re: [PATCH v17 092/116] KVM: TDX: Handle TDX PV HLT hypercall
Quoted 89 lines · Sean Christopherson
Quoted 72 lines · Chao Gao
Quoted 49 lines · Sean Christopherson
Quoted 33 lines · On Tue, Nov 07, 2023, [email protected] wrote:
From: Isaku Yamahata <[email protected]>
Wire up TDX PV HLT hypercall to the KVM backend function.
Signed-off-by Isaku Yamahata <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 42 +++++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 3 +++ 2 files changed, 44 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/tdx.c +14
-1@@ -662,7 +662,32 @@ void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)bool tdx_protected_apic_has_interrupt (struct kvm_vcpu *vcpu) { -return pi_has_pending_interrupt (vcpu); +bool ret =pi_has_pending_interrupt (vcpu); +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (ret || vcpu->arch.mp_state !=KVM_MP_STATE_HALTED ) +return true; + +if (tdx->interrupt_disabled_hlt) +return false; + +/* +* 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 arch/x86/kvm/vmx/tdx.c void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 688
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+* TDCALL completes before interrupts are enabled. +* +* TDX modules sets RVI while in an STI interrupt shadow. +* - TDExit(typically TDG.VP.VMCALL<HLT>) from the guest to TDX module. +* 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 TDExit nevertheless, to a clueless hypervisor that +* has no way to glean either RVI or PPR. WTF. Seriously, what in the absolute hell is going on. I reported this internally four ***YEARS*** ago. This is not some obscure theoretical edge case, this is core functionality and it's completely broken garbage.
Quoted 6 lines
NAK. Hard NAK. Fix the TDX module, full stop.
Even worse, TDX 1.5 apparently _already_ has the necessary logic for dealing with interrupts that are pending in RVI when handling NESTED VM-Enter. Really!?!?! Y'all went and added nested virtualization support of some kind, but can't find the time to get the basics right?
We actually fixed the TDX module. See 11.9.5. Pending Virtual Interrupt Delivery Indication in TDX module 1.5 spec [1]
The host VMM can detect whether a virtual interrupt is pending delivery to a VCPU in the Virtual APIC page, using TDH.VP.RD to read the
VCPU_STATE_DETAILSTDVPS field.The typical use case is when the guest TD VCPU indicates to the host VMM, using TDG.VP.VMCALL, that it has no work to do and can be halted. The guest TD is expected to pass an “interrupt blocked” flag. The guest TD is expected to set this flag to 0 if and only if RFLAGS.IF is 1 or the TDCALL instruction that invokes TDG.VP.VMCALL immediately follows an STI instruction. If the “interrupt blocked” flag is 0, the host VMM can determine whether to re-schedule the guest TD VCPU based on
VCPU_STATE_DETAILS.Quoted 4 lines
Isaku, this patch didn't read
VCPU_STATE_DETAILS. Maybe you missed something during rebase? Regardingbuggy_hlt_workaround, do you aim to avoid readingVCPU_STATE_DETAILSas much as possible (because reading it via SEAMCALL is costly, ~3-4K cycles)?*sigh* Why only earth doesn't the TDX module simply compute VMXIP on TDVMCALL? It's literally one bit and one extra VMREAD. There are plenty of register bits available, and I highly doubt ~20 cycles in the TDVMCALL path will be noticeable, let alone problematic. Such functionality could even be added on top in a TDX module update, and Intel could even bill it as a performance optimization.
Eating 4k cycles in the HLT path isn't the end of the world, but it's far from optimal and it's just so incredibly wasteful. I wouldn't be surprised if the latency is measurable for certain workloads, which will lead to guests using idle=poll and/or other games being played in the guest.
Quoted 7 lines
And AFAICT, the TDX module doesn't support HLT passthrough, so fully dedicated CPUs can't even mitigate the pain that way.
Anyways, regarding the "workaround", my NAK stands. It has bad tradeoffs of its own, e.g. will result in spurious wakeups, and can't possibly work for VMs with passthrough devices. Not to mention that the implementation has several races and false positives.
I'll drop the last part and use TDH.VP.RD(VCPU_STATE_DETAILS) with TDX 1.5.
Signature · 1 line
Isaku Yamahata <[email protected]>>
Re: [PATCH v17 055/116] KVM: TDX: TDP MMU TDX support
In reply to [email protected]Quoted 74 lines · On Tue, Nov 07, 2023 at 06:56:21AM -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.
Propagate 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 shoot down 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.
For MMIO SPTE, the spte value changes as follows.
initial value (suppress VE bit is set)
-> Guest issues MMIO and triggers EPT violation
-> KVM updates SPTE value to MMIO value (suppress VE bit is cleared) -> Guest MMIO resumes. It triggers VE exception in guest TD -> Guest VE handler issues TDG.VP.VMCALL<MMIO>
-> KVM handles MMIO
-> Guest VE handler resumes its execution after MMIO instructionSigned-off-by Isaku Yamahata <[email protected]>---
v15 -> v16:
- Add the handling ofTD_ATTR_SEPT_VE_DISABLEcase.v14 -> v15:
- Implementedtdx_flush_tlb_current()
- Removed unnecessary invept intdx_flush_tlb(). It was carry over from the very old code base.--- arch/x86/kvm/mmu/spte.c | 3 +- arch/x86/kvm/vmx/main.c | 71 +++++++- arch/x86/kvm/vmx/tdx.c | 342 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 7 + arch/x86/kvm/vmx/x86_ops.h | 6 + 5 files changed, 424 insertions(+), 5 deletions(-)arch/x86/kvm/mmu/spte.c +2
-1@@ -74,7 +74,8 @@ 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 (!vcpu->kvm->arch.shadow_mmio_value); +WARN_ON_ONCE (!vcpu->kvm->arch.shadow_mmio_value && + !kvm_gfn_shared_mask (vcpu->kvm)); access &= shadow_mmio_access_mask; spte |= vcpu->kvm->arch.shadow_mmio_value | access;arch/x86/kvm/vmx/main.c +1
-0@@ -28,6 +28,7 @@ static int vt_max_vcpus(struct kvm *kvm)return kvm->max_vcpus; } +static int vt_flush_remote_tlbs (struct kvm *kvm);static int vt_hardware_enable (void ) {
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 80
Quoted 10 lines
arch/x86/kvm/vmx/main.c +7
-0@@ -70,8 +71,22 @@ static __init int vt_hardware_setup(void)pr_warn_ratelimited ("TDX requires mmio caching. Please enable mmio caching for TDX.\n" ); } +/* +* TDX KVM overrides flush_remote_tlbs method and assumes +* flush_remote_tlbs_range = NULL that falls back to +* flush_remote_tlbs. Disable TDX if there are conflicts. +*/ +if (vt_x86_ops.flush_remote_tlbs || + vt_x86_ops.flush_remote_tlbs_range) {
these 2 fields are defined for CONFIG_HYPERV, so this leads to compiling errors when !CONFIG_HYPERV.
Is that possible to select CONFIG_HYPERV if this is *must* have dependency ?
arch/x86/kvm/vmx/main.c static __init int vt_hardware_setup(void) · line 88
Quoted 8 lines
arch/x86/kvm/vmx/main.c +6
-0+ enable_tdx = false; +pr_warn_ratelimited ("TDX requires baremetal. Not Supported on VMM guest.\n" ); + } + enable_tdx = enable_tdx && !tdx_hardware_setup (&vt_x86_ops); +if (enable_tdx) + vt_x86_ops.flush_remote_tlbs = vt_flush_remote_tlbs;
Ditto.
Quoted 510 lines
arch/x86/kvm/vmx/main.c +53
-4+return 0 ; }@@ -154,6 +169,54 @@ static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmx_vcpu_reset (vcpu, init_event); } +static void vt_flush_tlb_all (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_flush_tlb (vcpu); +return ; + } + +vmx_flush_tlb_all (vcpu); +} + +static void vt_flush_tlb_current (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) { +tdx_flush_tlb_current (vcpu); +return ; + } + +vmx_flush_tlb_current (vcpu); +} + +static int vt_flush_remote_tlbs (struct kvm *kvm) +{ +if (is_td (kvm)) +return tdx_sept_flush_remote_tlbs (kvm); + +/* +* fallback to KVM_REQ_TLB_FLUSH. +* See kvm_arch_flush_remote_tlb() and kvm_flush_remote_tlbs(). +*/ +return -EOPNOTSUPP ; +} + +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) {@@ -245,10 +308,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .set_rflags = vmx_set_rflags, .get_if_flag = vmx_get_if_flag, - .flush_tlb_all = vmx_flush_tlb_all, - .flush_tlb_current = vmx_flush_tlb_current, - .flush_tlb_gva = vmx_flush_tlb_gva, - .flush_tlb_guest = vmx_flush_tlb_guest, + .flush_tlb_all = vt_flush_tlb_all, + .flush_tlb_current = vt_flush_tlb_current, + .flush_tlb_gva = vt_flush_tlb_gva, + .flush_tlb_guest = vt_flush_tlb_guest, .vcpu_pre_run = vmx_vcpu_pre_run, .vcpu_run = vmx_vcpu_run,arch/x86/kvm/vmx/tdx.c +342
-0@@ -7,6 +7,7 @@#include "x86_ops.h" #include "mmu.h" #include "tdx.h" +#include "vmx.h" #include "x86.h" #undef pr_fmt@@ -331,6 +332,22 @@ static int tdx_do_tdh_mng_key_config(void *param)int tdx_vm_init (struct kvm *kvm) { +/* +* Because guest TD is protected, VMM can't parse the instruction in TD. +* Instead, guest uses MMIO hypercall. For unmodified device driver, +* #VE needs to be injected for MMIO and #VE handler in TD converts MMIO +* instruction into MMIO hypercall. +* +* SPTE value for MMIO needs to be setup so that #VE is injected into +* TD instead of triggering EPT MISCONFIG. +* - RWX=0 so that EPT violation is triggered. +* - suppress #VE bit is cleared to inject #VE. +*/ +kvm_mmu_set_mmio_spte_value (kvm,0 ); + +/* TODO: Enable 2mb and 1gb large page support. */ + kvm->arch.tdp_max_page_level =PG_LEVEL_4K ; +/* * This function initializes only KVM software construct. It doesn't * initialize TDX stuff, e.g. TDCS, TDR, TDCX, HKID etc. @@ -430,6 +447,285 @@ 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_unpin (struct kvm *kvm,kvm_pfn_t pfn) +{ +struct page *page =pfn_to_page (pfn); + +put_page (page); +} + +static int tdx_sept_page_aug (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); +union tdx_sept_level_state level_state; +hpa_t hpa =pfn_to_hpa (pfn); +gpa_t gpa =gfn_to_gpa (gfn); +struct tdx_module_args out; +union tdx_sept_entry entry; +u64 err; + + err =tdh_mem_page_aug (kvm_tdx->tdr_pa, gpa, hpa, &out); +if (unlikely (err ==TDX_ERROR_SEPT_BUSY )) { +tdx_unpin (kvm, pfn); +return -EAGAIN ; + } +if (unlikely (err == (TDX_EPT_ENTRY_STATE_INCORRECT |TDX_OPERAND_ID_RCX ))) { + entry.raw = out.rcx; + level_state.raw = out.rdx; +if (level_state.level == tdx_level && + level_state.state ==TDX_SEPT_PENDING && + entry.leaf && entry.pfn == pfn && entry.sve) { +tdx_unpin (kvm, gfn, pfn, level); +WARN_ON_ONCE (!(to_kvm_tdx (kvm)->attributes & +TDX_TD_ATTR_SEPT_VE_DISABLE )); +return -EAGAIN ; + } + } +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_PAGE_AUG , err, &out); +tdx_unpin (kvm, pfn); +return -EIO ; + } + +return 0 ; +} + +static int 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); + +/* TODO: handle large pages. */ +if (KVM_BUG_ON (level !=PG_LEVEL_4K , kvm)) +return -EINVAL ; + +/* +* Because restricted mem doesn't support page migration with +* a_ops->migrate_page (yet), no callback isn't triggered for KVM on +* page migration. Until restricted mem supports page migration, +* prevent page migration. +* TODO: Once restricted mem introduces callback on page migration, +* implement it and remove get_page/put_page(). +*/ +get_page (pfn_to_page (pfn)); + +if (likely (is_td_finalized (kvm_tdx))) +return tdx_sept_page_aug (kvm, gfn, level, pfn); + +/* TODO: tdh_mem_page_add() comes here for the initial memory. */ + +return 0 ; +} + +static int 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); +struct tdx_module_args out; +gpa_t gpa =gfn_to_gpa (gfn); +hpa_t hpa =pfn_to_hpa (pfn); +hpa_t hpa_with_hkid; +u64 err; + +/* TODO: handle large pages. */ +if (KVM_BUG_ON (level !=PG_LEVEL_4K , kvm)) +return -EINVAL ; + +if (unlikely (!is_hkid_assigned (kvm_tdx))) { +/* +* The HKID assigned to this TD was already freed and cache +* was already flushed. We don't have to flush again. +*/ + err =tdx_reclaim_page (hpa); +if (KVM_BUG_ON (err, kvm)) +return -EIO ; +tdx_unpin (kvm, pfn); +return 0 ; + } + +do { +/* +* When zapping private page, write lock is held. So no race +* condition with other vcpu sept operation. Race only with +* TDH.VP.ENTER. +*/ + err =tdh_mem_page_remove (kvm_tdx->tdr_pa, gpa, tdx_level, &out); + }while (unlikely (err ==TDX_ERROR_SEPT_BUSY )); +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_PAGE_REMOVE , err, &out); +return -EIO ; + } + + hpa_with_hkid =set_hkid_to_hpa (hpa, (u16 )kvm_tdx->hkid); +do { +/* +* TDX_OPERAND_BUSY can happen on locking PAMT entry. Because +* this page was removed above, other thread shouldn't be +* repeatedly operating on this page. Just retry loop. +*/ + err =tdh_phymem_page_wbinvd (hpa_with_hkid); + }while (unlikely (err == (TDX_OPERAND_BUSY |TDX_OPERAND_ID_RCX ))); +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_PHYMEM_PAGE_WBINVD , err,NULL ); +return -EIO ; + } +tdx_clear_page (hpa); +tdx_unpin (kvm, pfn); +return 0 ; +} + +static int tdx_sept_link_private_spt (struct kvm *kvm,gfn_t gfn, +enum pg_level level,void *private_spt) +{ +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 (private_spt); +struct tdx_module_args out; +u64 err; + + err =tdh_mem_sept_add (kvm_tdx->tdr_pa, gpa, tdx_level, hpa, &out); +if (unlikely (err ==TDX_ERROR_SEPT_BUSY )) +return -EAGAIN ; +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_SEPT_ADD , err, &out); +return -EIO ; + } + +return 0 ; +} + +static int 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) &KVM_HPAGE_MASK (level); +struct tdx_module_args out; +u64 err; + +/* This can be called when destructing guest TD after freeing HKID. */ +if (unlikely (!is_hkid_assigned (kvm_tdx))) +return 0 ; + +/* For now large page isn't supported yet. */ +WARN_ON_ONCE (level !=PG_LEVEL_4K ); + err =tdh_mem_range_block (kvm_tdx->tdr_pa, gpa, tdx_level, &out); +if (unlikely (err ==TDX_ERROR_SEPT_BUSY )) +return -EAGAIN ; +if (KVM_BUG_ON (err, kvm)) { +pr_tdx_error (TDH_MEM_RANGE_BLOCK , err, &out); +return -EIO ; + } +return 0 ; +} + +/* +* TLB shoot down procedure: +* There is a global epoch counter and each vcpu has local epoch counter. +* - TDH.MEM.RANGE.BLOCK(TDR. level, range) on one vcpu +* This blocks the subsequenct creation of TLB translation on that range. +* This corresponds to clear the present bit(all RXW) in EPT entry +* - TDH.MEM.TRACK(TDR): advances the epoch counter which is global. +* - IPI to remote vcpus +* - TDExit and re-entry with TDH.VP.ENTER on remote vcpus +* - On re-entry, TDX module compares the local epoch counter with the global +* epoch counter. If the local epoch counter is older than the global epoch +* counter, update the local epoch counter and flushes TLB. +*/ +static void tdx_track (struct kvm *kvm) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); +u64 err; + +KVM_BUG_ON (!is_hkid_assigned (kvm_tdx), kvm); +/* If TD isn't finalized, it's before any vcpu running. */ +if (unlikely (!is_td_finalized (kvm_tdx))) +return ; + +/* +* tdx_flush_tlb() waits for this function to issue TDH.MEM.TRACK() by +* the counter. The counter is used instead of bool because multiple +* TDH_MEM_TRACK() can be issued concurrently by multiple vcpus. +*/ +atomic_inc (&kvm_tdx->tdh_mem_track); +/* +* KVM_REQ_TLB_FLUSH waits for the empty IPI handler, ack_flush(), with +* KVM_REQUEST_WAIT. +*/ +kvm_make_all_cpus_request (kvm,KVM_REQ_TLB_FLUSH ); + +do { +/* +* kvm_flush_remote_tlbs() doesn't allow to return error and +* retry. +*/ + err =tdh_mem_track (kvm_tdx->tdr_pa); + }while (unlikely ((err &TDX_SEAMCALL_STATUS_MASK ) ==TDX_OPERAND_BUSY )); + +/* Release remote vcpu waiting for TDH.MEM.TRACK in tdx_flush_tlb(). */ +atomic_dec (&kvm_tdx->tdh_mem_track); + +if (KVM_BUG_ON (err, kvm)) +pr_tdx_error (TDH_MEM_TRACK , err,NULL ); + +} + +static int tdx_sept_free_private_spt (struct kvm *kvm,gfn_t gfn, +enum pg_level level,void *private_spt) +{ +struct kvm_tdx *kvm_tdx =to_kvm_tdx (kvm); + +/* +* The HKID assigned to this TD was already freed and cache was +* already flushed. We don't have to flush again. +*/ +if (!is_hkid_assigned (kvm_tdx)) +return tdx_reclaim_page (__pa (private_spt)); + +/* +* free_private_spt() is (obviously) called when a shadow page is being +* zapped. KVM doesn't (yet) zap private SPs while the TD is active. +* Note: This function is for private shadow page. Not for private +* guest page. private guest page can be zapped during TD is active. +* shared <-> private conversion and slot move/deletion. +*/ +KVM_BUG_ON (is_hkid_assigned (kvm_tdx), kvm); +return -EINVAL ; +} + +int tdx_sept_flush_remote_tlbs (struct kvm *kvm) +{ +if (unlikely (!is_td (kvm))) +return -EOPNOTSUPP ; + +if (is_hkid_assigned (to_kvm_tdx (kvm))) +tdx_track (kvm); + +return 0 ; +} + +static int tdx_sept_remove_private_spte (struct kvm *kvm,gfn_t gfn, +enum pg_level level,kvm_pfn_t pfn) +{ +/* +* 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: Call TDH.MEM.TRACK() only when we have called +* TDH.MEM.RANGE.BLOCK(), but not call TDH.MEM.TRACK() yet. +*/ +if (is_hkid_assigned (to_kvm_tdx (kvm))) +tdx_track (kvm); + +return tdx_sept_drop_private_spte (kvm, gfn, level, pfn); +} +static int tdx_get_capabilities (struct kvm_tdx_cmd *cmd) {struct kvm_tdx_capabilities __user *user_caps;@@ -895,6 +1191,39 @@ static int tdx_td_init(struct kvm *kvm, struct kvm_tdx_cmd *cmd)return ret; } +void tdx_flush_tlb (struct kvm_vcpu *vcpu) +{ +/* +* Don't need to flush shared EPTP: +* "TD VCPU TLB Address Spaced Identifier" in the TDX module spec: +* The TLB entries for TD are tagged with: +* SEAM (1 bit) +* VPID +* Secure EPT root (51:12 bits) with HKID = 0 +* PCID +* for *both* Secure-EPT and Shared-EPT. +* TLB flush with Secure-EPT root by tdx_track() results in flushing +* the conversion of both Secure-EPT and Shared-EPT. +*/ + +/* +* See tdx_track(). Wait for tlb shootdown initiater to finish +* TDH_MEM_TRACK() so that shared-EPT/secure-EPT TLB is flushed +* on the next TDENTER. +*/ +while (atomic_read (&to_kvm_tdx (vcpu->kvm)->tdh_mem_track)) +cpu_relax (); +} + +void tdx_flush_tlb_current (struct kvm_vcpu *vcpu) +{ +/* +* flush_tlb_current() is used only the first time for the vcpu to run. +* As it isn't performance critical, keep this function simple. +*/ +tdx_track (vcpu->kvm); +} +int tdx_vm_ioctl (struct kvm *kvm,void __user *argp) {struct kvm_tdx_cmd tdx_cmd;@@ -1160,8 +1489,21 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)on_each_cpu (vmx_off, &vmx_tdx.vmx_enabled, true);cpus_read_unlock ();free_cpumask_var (vmx_tdx.vmx_enabled); +if (r) + goto out; + + x86_ops->link_private_spt = tdx_sept_link_private_spt; + x86_ops->free_private_spt = tdx_sept_free_private_spt; + x86_ops->set_private_spte = tdx_sept_set_private_spte; + x86_ops->remove_private_spte = tdx_sept_remove_private_spte; + x86_ops->zap_private_spte = tdx_sept_zap_private_spte; + +return 0 ; out: +/* kfree() accepts NULL. */ +kfree (tdx_mng_key_config_lock); + tdx_mng_key_config_lock =NULL ;return r; }arch/x86/kvm/vmx/tdx.h +7
-0@@ -18,6 +18,7 @@ struct kvm_tdx {int hkid;bool finalized; +atomic_t tdh_mem_track;u64 tsc_offset; };@@ -165,6 +166,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_ONCE (level ==PG_LEVEL_NONE ); +return level -1 ; +} +#else struct kvm_tdx { struct kvm kvm;arch/x86/kvm/vmx/x86_ops.h +6
-1@@ -153,6 +153,9 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);int tdx_vcpu_ioctl (struct kvm_vcpu *vcpu,void __user *argp); +void tdx_flush_tlb (struct kvm_vcpu *vcpu); +void tdx_flush_tlb_current (struct kvm_vcpu *vcpu); +int tdx_sept_flush_remote_tlbs (struct kvm *kvm);void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);#else static inline int tdx_hardware_setup (struct kvm_x86_ops *x86_ops) {return -EOPNOTSUPP ; }@@ -176,6 +179,9 @@ static inline void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) {}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_flush_tlb_current (struct kvm_vcpu *vcpu) {} +static inline int tdx_sept_flush_remote_tlbs (struct kvm *kvm) {return 0 ; }static inline void tdx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {}#endif --2.25.1