Skip to content

Commit 8d032b6

Browse files
yamahatabonzini
authored andcommitted
KVM: TDX: create/destroy VM structure
Implement managing the TDX private KeyID to implement, create, destroy and free for a TDX guest. When creating at TDX guest, assign a TDX private KeyID for the TDX guest for memory encryption, and allocate pages for the guest. These are used for the Trust Domain Root (TDR) and Trust Domain Control Structure (TDCS). On destruction, free the allocated pages, and the KeyID. Before tearing down the private page tables, TDX requires the guest TD to be destroyed by reclaiming the KeyID. Do it in the vm_pre_destroy() kvm_x86_ops hook. The TDR control structures can be freed in the vm_destroy() hook, which runs last. Co-developed-by: Tony Lindgren <tony.lindgren@linux.intel.com> Signed-off-by: Tony Lindgren <tony.lindgren@linux.intel.com> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com> Co-developed-by: Sean Christopherson <sean.j.christopherson@intel.com> Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com> Co-developed-by: Kai Huang <kai.huang@intel.com> Signed-off-by: Kai Huang <kai.huang@intel.com> Co-developed-by: Yan Zhao <yan.y.zhao@intel.com> Signed-off-by: Yan Zhao <yan.y.zhao@intel.com> Co-developed-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com> --- - Fix build issue in kvm-coco-queue - Init ret earlier to fix __tdx_td_init() error handling. (Chao) - Standardize -EAGAIN for __tdx_td_init() retry errors (Rick) Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 61bb282 commit 8d032b6

File tree

8 files changed

+476
-3
lines changed

8 files changed

+476
-3
lines changed

arch/x86/include/asm/kvm-x86-ops.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ KVM_X86_OP(has_emulated_msr)
2121
KVM_X86_OP(vcpu_after_set_cpuid)
2222
KVM_X86_OP(vm_init)
2323
KVM_X86_OP_OPTIONAL(vm_destroy)
24+
KVM_X86_OP_OPTIONAL(vm_pre_destroy)
2425
KVM_X86_OP_OPTIONAL_RET0(vcpu_precreate)
2526
KVM_X86_OP(vcpu_create)
2627
KVM_X86_OP(vcpu_free)

arch/x86/include/asm/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,7 @@ struct kvm_x86_ops {
16651665
unsigned int vm_size;
16661666
int (*vm_init)(struct kvm *kvm);
16671667
void (*vm_destroy)(struct kvm *kvm);
1668+
void (*vm_pre_destroy)(struct kvm *kvm);
16681669

16691670
/* Create, but do not attach this VCPU */
16701671
int (*vcpu_precreate)(struct kvm *kvm);

arch/x86/kvm/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ config KVM_SW_PROTECTED_VM
9494
config KVM_INTEL
9595
tristate "KVM for Intel (and compatible) processors support"
9696
depends on KVM && IA32_FEAT_CTL
97+
select KVM_GENERIC_PRIVATE_MEM if INTEL_TDX_HOST
98+
select KVM_GENERIC_MEMORY_ATTRIBUTES if INTEL_TDX_HOST
9799
help
98100
Provides support for KVM on processors equipped with Intel's VT
99101
extensions, a.k.a. Virtual Machine Extensions (VMX).

arch/x86/kvm/vmx/main.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ static __init int vt_hardware_setup(void)
4141
return 0;
4242
}
4343

44+
static int vt_vm_init(struct kvm *kvm)
45+
{
46+
if (is_td(kvm))
47+
return tdx_vm_init(kvm);
48+
49+
return vmx_vm_init(kvm);
50+
}
51+
52+
static void vt_vm_pre_destroy(struct kvm *kvm)
53+
{
54+
if (is_td(kvm))
55+
return tdx_mmu_release_hkid(kvm);
56+
}
57+
58+
static void vt_vm_destroy(struct kvm *kvm)
59+
{
60+
if (is_td(kvm))
61+
return tdx_vm_destroy(kvm);
62+
63+
vmx_vm_destroy(kvm);
64+
}
65+
4466
static int vt_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
4567
{
4668
if (!is_td(kvm))
@@ -72,8 +94,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
7294
.has_emulated_msr = vmx_has_emulated_msr,
7395

7496
.vm_size = sizeof(struct kvm_vmx),
75-
.vm_init = vmx_vm_init,
76-
.vm_destroy = vmx_vm_destroy,
97+
98+
.vm_init = vt_vm_init,
99+
.vm_pre_destroy = vt_vm_pre_destroy,
100+
.vm_destroy = vt_vm_destroy,
77101

78102
.vcpu_precreate = vmx_vcpu_precreate,
79103
.vcpu_create = vmx_vcpu_create,

0 commit comments

Comments
 (0)