KVM: TDX: create/free TDX vcpu structure
authorIsaku Yamahata <isaku.yamahata@intel.com>
Wed, 30 Oct 2024 19:00:35 +0000 (12:00 -0700)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 14 Mar 2025 18:20:51 +0000 (14:20 -0400)
Implement vcpu related stubs for TDX for create, reset and free.

For now, create only the features that do not require the TDX SEAMCALL.
The TDX specific vcpu initialization will be handled by KVM_TDX_INIT_VCPU.

Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
---
 - Use lapic_in_kernel() (Nikolay Borisov)
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/vmx/main.c
arch/x86/kvm/vmx/tdx.c
arch/x86/kvm/vmx/x86_ops.h

index e0806dbe7b87e436ee7e1033d78484dc13274f87..a840a84815898f5eefd3fa60fe1020c83198ccde 100644 (file)
@@ -64,6 +64,40 @@ static void vt_vm_destroy(struct kvm *kvm)
        vmx_vm_destroy(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);
+}
+
+static void vt_vcpu_free(struct kvm_vcpu *vcpu)
+{
+       if (is_td_vcpu(vcpu)) {
+               tdx_vcpu_free(vcpu);
+               return;
+       }
+
+       vmx_vcpu_free(vcpu);
+}
+
+static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
+{
+       if (is_td_vcpu(vcpu))
+               return;
+
+       vmx_vcpu_reset(vcpu, init_event);
+}
+
 static int vt_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
 {
        if (!is_td(kvm))
@@ -100,10 +134,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
        .vm_pre_destroy = vt_vm_pre_destroy,
        .vm_destroy = vt_vm_destroy,
 
-       .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,
index 303ecd30b8e25bd9105eeb32840f6c13741ea60d..e7bcea016f6401b350679ad8c3aba6f4ad508722 100644 (file)
@@ -4,6 +4,7 @@
 #include <asm/tdx.h>
 #include "capabilities.h"
 #include "x86_ops.h"
+#include "lapic.h"
 #include "tdx.h"
 
 #pragma GCC poison to_vmx
@@ -406,6 +407,45 @@ 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);
+
+       if (kvm_tdx->state != TD_STATE_INITIALIZED)
+               return -EIO;
+
+       /* TDX module mandates APICv, which requires an in-kernel local APIC. */
+       if (!lapic_in_kernel(vcpu))
+               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;
+
+       /* KVM can't change TSC offset/multiplier as TDX module manages them. */
+       vcpu->arch.guest_tsc_protected = true;
+       vcpu->arch.tsc_offset = kvm_tdx->tsc_offset;
+       vcpu->arch.l1_tsc_offset = vcpu->arch.tsc_offset;
+       vcpu->arch.tsc_scaling_ratio = kvm_tdx->tsc_multiplier;
+       vcpu->arch.l1_tsc_scaling_ratio = kvm_tdx->tsc_multiplier;
+
+       vcpu->arch.guest_state_protected =
+               !(to_kvm_tdx(vcpu->kvm)->attributes & TDX_TD_ATTR_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. */
+}
+
 static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd)
 {
        const struct tdx_sys_info_td_conf *td_conf = &tdx_sysinfo->td_conf;
index 85c78639d476f2fcd01b70d8f7286e58b0cce733..c57a69a9c7d06ebdde5032e0a091ee012d1e5924 100644 (file)
@@ -126,11 +126,17 @@ int tdx_vm_init(struct kvm *kvm);
 void tdx_mmu_release_hkid(struct kvm *kvm);
 void tdx_vm_destroy(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);
 #else
 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_destroy(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) {}
 #endif
 
 #endif /* __KVM_X86_VMX_X86_OPS_H */