Skip to content

x86: Add TDX support#375

Open
00xc wants to merge 7 commits into
rust-vmm:mainfrom
00xc:tdx-v2
Open

x86: Add TDX support#375
00xc wants to merge 7 commits into
rust-vmm:mainfrom
00xc:tdx-v2

Conversation

@00xc

@00xc 00xc commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Summary of the PR

Add all the required interfaces to be able to create TDX VMs. This includes:

  • Updating kvm-bindings to include new structures and definitions.
  • Add FAM wrappers for the TDX structs that need it.
  • Enable using KVM_CAP_VM_TYPES to detect if KVM supports TDX.
  • Enable handling KVM_EXIT_TDX and accessing the right fields in kvm_run.
  • Add the required TDX-specific KVM_MEMORY_ENCRYPT_OP variants (i.e. the TDX side of VmFd::encrypt_op_sev().

This closes #374.

Requirements

Before submitting your PR, please make sure you addressed the following
requirements:

  • All commits in this PR have Signed-Off-By trailers (with
    git commit -s), and the commit message has max 60 characters for the
    summary and max 75 characters for each description line.
  • All added/changed functionality has a corresponding unit/integration
    test.
  • All added/changed public-facing functionality has entries in the "Upcoming
    Release" section of CHANGELOG.md (if no such section exists, please create one).
  • Any newly added unsafe code is properly documented.

@00xc 00xc force-pushed the tdx-v2 branch 2 times, most recently from bf9d022 to 321a019 Compare June 3, 2026 15:38
@00xc

00xc commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

The unit test failure on arm64 is unrelated. I opened #376 to fix it.

@00xc

00xc commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

The unit test failure on arm64 is unrelated. I opened #376 to fix it.

...and the x86 failures should be fixed in #377

00xc added 7 commits June 16, 2026 17:50
The kernel uapi headers recently started making extensive use of the
__DECLARE_FLEX_ARRAY() macro for flexible array members. This is defined
as:

    #define __DECLARE_FLEX_ARRAY(TYPE, NAME)	\
    	struct { \
    		struct { } __empty_ ## NAME; \
    		TYPE NAME[]; \
    	}
    #endif

When generating bindings for this code, bindgen will end up generating a
lof of __empty_xxx structures, making things less readable and usable.

For example, before the use of __DECLARE_FLEX_ARRAY(), we would get the
following on the Rust side:

    pub struct kvm_msrs {
        pub nmsrs: __u32,
        pub pad: __u32,
        pub entries: __IncompleteArrayField<kvm_msr_entry>,
    }

But with __DECLARE_FLEX_ARRAY() on this struct, we get:

    pub struct kvm_msrs {
         pub nmsrs: __u32,
         pub pad: __u32,
         pub __bindgen_anon_1: kvm_msrs__bindgen_ty_1,
    }
    #[repr(C)]
    #[derive(Debug, Default)]
    pub struct kvm_msrs__bindgen_ty_1 {
        pub __empty_entries: kvm_msrs__bindgen_ty_1__bindgen_ty_1,
        pub entries: __IncompleteArrayField<kvm_msr_entry>,
    }

The culprit macro is however defined as follows for C++:

    #define __DECLARE_FLEX_ARRAY(T, member)	\
    	T member[0]

Which is exactly what kvm-bindings wants. Document that -D__cplusplus
should be used to make new bindings updates less intrusive.

Signed-off-by: Carlos López <clopez@suse.de>
Update the KVM bindings to the most recent kernel version to bring in
new constants and structures, mostly related to x86 CoCo (SEV/TDX).

Signed-off-by: Carlos López <clopez@suse.de>
kvm_tdx_capabilities and kvm_tdx_init_vm contain a kvm_cpuid2 field at
the end, which itself is a flexible array member. Manually implement
FamStruct for these types, which will simplify using the TDX ioctls that
consume these structs (KVM_TDX_CAPABILITIES / KVM_TDX_INIT_VM).

Signed-off-by: Carlos López <clopez@suse.de>
Add a missing capability to the Cap enum, which allows querying the
VM types that KVM supports.

Signed-off-by: Carlos López <clopez@suse.de>
Support KVM_EXIT_TDX, KVM's vmexit for TDX guest requests. Add the
required Rust structures to adapt the parameters for the exit. As of
this commit, this vmexit cannot be used, as the required ioctls to
create a TDX VM are not yet implemented.

Signed-off-by: Carlos López <clopez@suse.de>
Add the TDX equivalents of VmFd::encrypt_op_sev(). Note that, for TDX,
KVM_MEMORY_ENCRYPT_OP also operates on the vCPU fd, so add a similar
method for VcpuFd.

This is the final piece to allow users to create TDX VMs, so update the
changelog.

Signed-off-by: Carlos López <clopez@suse.de>
Signed-off-by: Carlos López <clopez@suse.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No support for KVM_EXIT_TDX

1 participant