Skip to content

Commit

Permalink
KVM: TDX: create/destroy VM structure
Browse files Browse the repository at this point in the history
As the first step to create TDX guest, create/destroy VM struct.  Assign
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.

Add a second kvm_x86_ops hook in kvm_arch_vm_destroy() to support TDX's
destruction path, which needs to first put the VM into a teardown state,
then free per-vCPU resources, and finally free per-VM resources.

Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
  • Loading branch information
Sean Christopherson authored and yamahata committed Jan 21, 2022
1 parent 1758528 commit c24ea1a
Show file tree
Hide file tree
Showing 8 changed files with 387 additions and 1 deletion.
1 change: 1 addition & 0 deletions arch/x86/include/asm/kvm-x86-ops.h
Expand Up @@ -21,6 +21,7 @@ KVM_X86_OP(vcpu_after_set_cpuid)
KVM_X86_OP(is_vm_type_supported)
KVM_X86_OP(vm_init)
KVM_X86_OP_NULL(vm_destroy)
KVM_X86_OP_NULL(vm_free)
KVM_X86_OP(vcpu_create)
KVM_X86_OP(vcpu_free)
KVM_X86_OP(vcpu_reset)
Expand Down
1 change: 1 addition & 0 deletions arch/x86/include/asm/kvm_host.h
Expand Up @@ -1318,6 +1318,7 @@ struct kvm_x86_ops {
unsigned int vm_size;
int (*vm_init)(struct kvm *kvm);
void (*vm_destroy)(struct kvm *kvm);
void (*vm_free)(struct kvm *kvm);

/* Create, but do not attach this VCPU */
int (*vcpu_create)(struct kvm_vcpu *vcpu);
Expand Down
27 changes: 26 additions & 1 deletion arch/x86/kvm/vmx/main.c
Expand Up @@ -3,6 +3,7 @@

#include "x86_ops.h"
#include "vmx.h"
#include "tdx.h"
#include "nested.h"
#include "pmu.h"

Expand All @@ -28,6 +29,28 @@ static __init int vt_hardware_setup(void)
return 0;
}

static int vt_vm_init(struct kvm *kvm)
{
if (kvm->arch.vm_type == KVM_X86_TDX_VM)
return tdx_vm_init(kvm);

return vmx_vm_init(kvm);
}

static void vt_vm_destroy(struct kvm *kvm)
{
if (is_td(kvm))
return tdx_vm_teardown(kvm);
}

static void vt_vm_free(struct kvm *kvm)
{
if (is_td(kvm)) {
tdx_vm_free(kvm);
return;
}
}

static int vt_mem_enc_op_dev(void __user *argp)
{
if (!enable_tdx)
Expand All @@ -54,7 +77,9 @@ struct kvm_x86_ops vt_x86_ops __initdata = {

.is_vm_type_supported = vt_is_vm_type_supported,
.vm_size = sizeof(struct kvm_vmx),
.vm_init = vmx_vm_init,
.vm_init = vt_vm_init,
.vm_destroy = vt_vm_destroy,
.vm_free = vt_vm_free,

.vcpu_create = vmx_create_vcpu,
.vcpu_free = vmx_free_vcpu,
Expand Down

0 comments on commit c24ea1a

Please sign in to comment.