Skip to content

Commit b2aaf38

Browse files
yamahatabonzini
authored andcommitted
KVM: TDX: Add place holder for TDX VM specific mem_enc_op ioctl
KVM_MEMORY_ENCRYPT_OP was introduced for VM-scoped operations specific for guest state-protected VM. It defined subcommands for technology-specific operations under KVM_MEMORY_ENCRYPT_OP. Despite its name, the subcommands are not limited to memory encryption, but various technology-specific operations are defined. It's natural to repurpose KVM_MEMORY_ENCRYPT_OP for TDX specific operations and define subcommands. Add a place holder function for TDX specific VM-scoped ioctl as mem_enc_op. TDX specific sub-commands will be added to retrieve/pass TDX specific parameters. Make mem_enc_ioctl non-optional as it's always filled. Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com> Co-developed-by: Tony Lindgren <tony.lindgren@linux.intel.com> Signed-off-by: Tony Lindgren <tony.lindgren@linux.intel.com> Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com> --- - Drop the misleading "defined for consistency" line. It's a copy-paste error introduced in the earlier patches. Earlier there was padding at the end to match struct kvm_sev_cmd size. (Tony) Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent e4aa6f6 commit b2aaf38

File tree

6 files changed

+72
-5
lines changed

6 files changed

+72
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ KVM_X86_OP(leave_smm)
125125
KVM_X86_OP(enable_smi_window)
126126
#endif
127127
KVM_X86_OP_OPTIONAL(dev_get_attr)
128-
KVM_X86_OP_OPTIONAL(mem_enc_ioctl)
128+
KVM_X86_OP(mem_enc_ioctl)
129129
KVM_X86_OP_OPTIONAL(mem_enc_register_region)
130130
KVM_X86_OP_OPTIONAL(mem_enc_unregister_region)
131131
KVM_X86_OP_OPTIONAL(vm_copy_enc_context_from)

arch/x86/include/uapi/asm/kvm.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,4 +927,27 @@ struct kvm_hyperv_eventfd {
927927
#define KVM_X86_SNP_VM 4
928928
#define KVM_X86_TDX_VM 5
929929

930+
/* Trust Domain eXtension sub-ioctl() commands. */
931+
enum kvm_tdx_cmd_id {
932+
KVM_TDX_CMD_NR_MAX,
933+
};
934+
935+
struct kvm_tdx_cmd {
936+
/* enum kvm_tdx_cmd_id */
937+
__u32 id;
938+
/* flags for sub-commend. If sub-command doesn't use this, set zero. */
939+
__u32 flags;
940+
/*
941+
* data for each sub-command. An immediate or a pointer to the actual
942+
* data in process virtual address. If sub-command doesn't use it,
943+
* set zero.
944+
*/
945+
__u64 data;
946+
/*
947+
* Auxiliary error code. The sub-command may return TDX SEAMCALL
948+
* status code in addition to -Exxx.
949+
*/
950+
__u64 hw_error;
951+
};
952+
930953
#endif /* _ASM_X86_KVM_H */

arch/x86/kvm/vmx/main.c

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

44+
static int vt_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
45+
{
46+
if (!is_td(kvm))
47+
return -ENOTTY;
48+
49+
return tdx_vm_ioctl(kvm, argp);
50+
}
51+
4452
#define VMX_REQUIRED_APICV_INHIBITS \
4553
(BIT(APICV_INHIBIT_REASON_DISABLED) | \
4654
BIT(APICV_INHIBIT_REASON_ABSENT) | \
@@ -193,6 +201,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
193201
.vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector,
194202

195203
.get_untagged_addr = vmx_get_untagged_addr,
204+
205+
.mem_enc_ioctl = vt_mem_enc_ioctl,
196206
};
197207

198208
struct kvm_x86_init_ops vt_init_ops __initdata = {

arch/x86/kvm/vmx/tdx.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <asm/cpufeature.h>
44
#include <asm/tdx.h>
55
#include "capabilities.h"
6+
#include "x86_ops.h"
67
#include "tdx.h"
78

89
#pragma GCC poison to_vmx
@@ -42,6 +43,37 @@ static __always_inline struct vcpu_tdx *to_tdx(struct kvm_vcpu *vcpu)
4243
return container_of(vcpu, struct vcpu_tdx, vcpu);
4344
}
4445

46+
int tdx_vm_ioctl(struct kvm *kvm, void __user *argp)
47+
{
48+
struct kvm_tdx_cmd tdx_cmd;
49+
int r;
50+
51+
if (copy_from_user(&tdx_cmd, argp, sizeof(struct kvm_tdx_cmd)))
52+
return -EFAULT;
53+
54+
/*
55+
* Userspace should never set hw_error. It is used to fill
56+
* hardware-defined error by the kernel.
57+
*/
58+
if (tdx_cmd.hw_error)
59+
return -EINVAL;
60+
61+
mutex_lock(&kvm->lock);
62+
63+
switch (tdx_cmd.id) {
64+
default:
65+
r = -EINVAL;
66+
goto out;
67+
}
68+
69+
if (copy_to_user(argp, &tdx_cmd, sizeof(struct kvm_tdx_cmd)))
70+
r = -EFAULT;
71+
72+
out:
73+
mutex_unlock(&kvm->lock);
74+
return r;
75+
}
76+
4577
static int tdx_online_cpu(unsigned int cpu)
4678
{
4779
unsigned long flags;

arch/x86/kvm/vmx/x86_ops.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,10 @@ void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu);
121121
#endif
122122
void vmx_setup_mce(struct kvm_vcpu *vcpu);
123123

124+
#ifdef CONFIG_KVM_INTEL_TDX
125+
int tdx_vm_ioctl(struct kvm *kvm, void __user *argp);
126+
#else
127+
static inline int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { return -EOPNOTSUPP; }
128+
#endif
129+
124130
#endif /* __KVM_X86_VMX_X86_OPS_H */

arch/x86/kvm/x86.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7289,10 +7289,6 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
72897289
goto out;
72907290
}
72917291
case KVM_MEMORY_ENCRYPT_OP: {
7292-
r = -ENOTTY;
7293-
if (!kvm_x86_ops.mem_enc_ioctl)
7294-
goto out;
7295-
72967292
r = kvm_x86_call(mem_enc_ioctl)(kvm, argp);
72977293
break;
72987294
}

0 commit comments

Comments
 (0)