Skip to content

Commit 9002f8c

Browse files
yamahatabonzini
authored andcommitted
KVM: TDX: create/free TDX vcpu structure
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>
1 parent 9934d7e commit 9002f8c

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

arch/x86/kvm/vmx/main.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,40 @@ static void vt_vm_destroy(struct kvm *kvm)
6464
vmx_vm_destroy(kvm);
6565
}
6666

67+
static int vt_vcpu_precreate(struct kvm *kvm)
68+
{
69+
if (is_td(kvm))
70+
return 0;
71+
72+
return vmx_vcpu_precreate(kvm);
73+
}
74+
75+
static int vt_vcpu_create(struct kvm_vcpu *vcpu)
76+
{
77+
if (is_td_vcpu(vcpu))
78+
return tdx_vcpu_create(vcpu);
79+
80+
return vmx_vcpu_create(vcpu);
81+
}
82+
83+
static void vt_vcpu_free(struct kvm_vcpu *vcpu)
84+
{
85+
if (is_td_vcpu(vcpu)) {
86+
tdx_vcpu_free(vcpu);
87+
return;
88+
}
89+
90+
vmx_vcpu_free(vcpu);
91+
}
92+
93+
static void vt_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
94+
{
95+
if (is_td_vcpu(vcpu))
96+
return;
97+
98+
vmx_vcpu_reset(vcpu, init_event);
99+
}
100+
67101
static int vt_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
68102
{
69103
if (!is_td(kvm))
@@ -100,10 +134,10 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
100134
.vm_pre_destroy = vt_vm_pre_destroy,
101135
.vm_destroy = vt_vm_destroy,
102136

103-
.vcpu_precreate = vmx_vcpu_precreate,
104-
.vcpu_create = vmx_vcpu_create,
105-
.vcpu_free = vmx_vcpu_free,
106-
.vcpu_reset = vmx_vcpu_reset,
137+
.vcpu_precreate = vt_vcpu_precreate,
138+
.vcpu_create = vt_vcpu_create,
139+
.vcpu_free = vt_vcpu_free,
140+
.vcpu_reset = vt_vcpu_reset,
107141

108142
.prepare_switch_to_guest = vmx_prepare_switch_to_guest,
109143
.vcpu_load = vmx_vcpu_load,

arch/x86/kvm/vmx/tdx.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <asm/tdx.h>
55
#include "capabilities.h"
66
#include "x86_ops.h"
7+
#include "lapic.h"
78
#include "tdx.h"
89

910
#pragma GCC poison to_vmx
@@ -406,6 +407,45 @@ int tdx_vm_init(struct kvm *kvm)
406407
return 0;
407408
}
408409

410+
int tdx_vcpu_create(struct kvm_vcpu *vcpu)
411+
{
412+
struct kvm_tdx *kvm_tdx = to_kvm_tdx(vcpu->kvm);
413+
414+
if (kvm_tdx->state != TD_STATE_INITIALIZED)
415+
return -EIO;
416+
417+
/* TDX module mandates APICv, which requires an in-kernel local APIC. */
418+
if (!lapic_in_kernel(vcpu))
419+
return -EINVAL;
420+
421+
fpstate_set_confidential(&vcpu->arch.guest_fpu);
422+
423+
vcpu->arch.efer = EFER_SCE | EFER_LME | EFER_LMA | EFER_NX;
424+
425+
vcpu->arch.cr0_guest_owned_bits = -1ul;
426+
vcpu->arch.cr4_guest_owned_bits = -1ul;
427+
428+
/* KVM can't change TSC offset/multiplier as TDX module manages them. */
429+
vcpu->arch.guest_tsc_protected = true;
430+
vcpu->arch.tsc_offset = kvm_tdx->tsc_offset;
431+
vcpu->arch.l1_tsc_offset = vcpu->arch.tsc_offset;
432+
vcpu->arch.tsc_scaling_ratio = kvm_tdx->tsc_multiplier;
433+
vcpu->arch.l1_tsc_scaling_ratio = kvm_tdx->tsc_multiplier;
434+
435+
vcpu->arch.guest_state_protected =
436+
!(to_kvm_tdx(vcpu->kvm)->attributes & TDX_TD_ATTR_DEBUG);
437+
438+
if ((kvm_tdx->xfam & XFEATURE_MASK_XTILE) == XFEATURE_MASK_XTILE)
439+
vcpu->arch.xfd_no_write_intercept = true;
440+
441+
return 0;
442+
}
443+
444+
void tdx_vcpu_free(struct kvm_vcpu *vcpu)
445+
{
446+
/* This is stub for now. More logic will come. */
447+
}
448+
409449
static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd)
410450
{
411451
const struct tdx_sys_info_td_conf *td_conf = &tdx_sysinfo->td_conf;

arch/x86/kvm/vmx/x86_ops.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,17 @@ int tdx_vm_init(struct kvm *kvm);
126126
void tdx_mmu_release_hkid(struct kvm *kvm);
127127
void tdx_vm_destroy(struct kvm *kvm);
128128
int tdx_vm_ioctl(struct kvm *kvm, void __user *argp);
129+
130+
int tdx_vcpu_create(struct kvm_vcpu *vcpu);
131+
void tdx_vcpu_free(struct kvm_vcpu *vcpu);
129132
#else
130133
static inline int tdx_vm_init(struct kvm *kvm) { return -EOPNOTSUPP; }
131134
static inline void tdx_mmu_release_hkid(struct kvm *kvm) {}
132135
static inline void tdx_vm_destroy(struct kvm *kvm) {}
133136
static inline int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { return -EOPNOTSUPP; }
137+
138+
static inline int tdx_vcpu_create(struct kvm_vcpu *vcpu) { return -EOPNOTSUPP; }
139+
static inline void tdx_vcpu_free(struct kvm_vcpu *vcpu) {}
134140
#endif
135141

136142
#endif /* __KVM_X86_VMX_X86_OPS_H */

0 commit comments

Comments
 (0)