[PATCH RFC 2/9] KVM: selftests: TDX: Add TDX IO writes test
From: Lisa Wang
Date: Fri Dec 04 2026 - 19:39:23 EST
From: Sagi Shahar <sagis@xxxxxxxxxx>
The test verifies legal and illegal IO writes from the guest to the
host.
Signed-off-by: Sagi Shahar <sagis@xxxxxxxxxx>
Co-developed-by: Lisa Wang <wyihan@xxxxxxxxxx>
Signed-off-by: Lisa Wang <wyihan@xxxxxxxxxx>
---
tools/testing/selftests/kvm/include/x86/tdx/tdx.h | 12 +++++++++
tools/testing/selftests/kvm/x86/tdx_vm_test.c | 33 +++++++++++++++++++++++
2 files changed, 45 insertions(+)
diff --git a/tools/testing/selftests/kvm/include/x86/tdx/tdx.h b/tools/testing/selftests/kvm/include/x86/tdx/tdx.h
index 9982aaaba885..f99f4f63407a 100644
--- a/tools/testing/selftests/kvm/include/x86/tdx/tdx.h
+++ b/tools/testing/selftests/kvm/include/x86/tdx/tdx.h
@@ -5,12 +5,24 @@
#include <linux/types.h>
/* TDX hypercall Leaf IDs */
+#define TDVMCALL_IO 30
#define TDVMCALL_GET_TD_VM_CALL_INFO 0x10000
#define TDVMCALL_MAP_GPA 0x10001
#define TDVMCALL_GET_QUOTE 0x10002
#define TDVMCALL_REPORT_FATAL_ERROR 0x10003
#define TDVMCALL_SETUP_EVENT_NOTIFY_INTERRUPT 0x10004
+/* TDX hypercall Status Codes (returned in R10) */
+#define TDVMCALL_STATUS_INVALID_OPERAND 0x8000000000000000ULL
+
+/* TDX hypercall direction arguments */
+#define TDVMCALL_IO_WRITE 1
+
u64 __tdvmcall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15);
+static inline u64 tdx_io_write(u16 port, u32 size, u64 data)
+{
+ return __tdvmcall(TDVMCALL_IO, size, TDVMCALL_IO_WRITE, port, data);
+}
+
#endif /* SELFTEST_KVM_TDX_TDX_H */
diff --git a/tools/testing/selftests/kvm/x86/tdx_vm_test.c b/tools/testing/selftests/kvm/x86/tdx_vm_test.c
index 6ab8ad52bd4e..07bfaad422fe 100644
--- a/tools/testing/selftests/kvm/x86/tdx_vm_test.c
+++ b/tools/testing/selftests/kvm/x86/tdx_vm_test.c
@@ -3,6 +3,7 @@
#include "processor.h"
#include "kvm_util.h"
#include "tdx/tdx_util.h"
+#include "tdx/tdx.h"
#include "ucall_common.h"
#include "kselftest_harness.h"
@@ -33,6 +34,38 @@ TDX_TEST(verify_td_lifecycle, guest_code_lifecycle)
TEST_ASSERT_EQ(get_ucall(vcpu, &uc), UCALL_DONE);
}
+#define TEST_IO_PORT 0x50
+#define TEST_IO_DATA_VALUE 0xABCDBABA
+
+static void guest_io_write(void)
+{
+ u64 ret;
+
+ ret = tdx_io_write(TEST_IO_PORT, sizeof(TEST_IO_DATA_VALUE),
+ TEST_IO_DATA_VALUE);
+ GUEST_ASSERT(!ret);
+ ret = tdx_io_write(TEST_IO_PORT, 8, TEST_IO_DATA_VALUE);
+ GUEST_ASSERT_EQ(ret, TDVMCALL_STATUS_INVALID_OPERAND);
+ GUEST_DONE();
+}
+
+TDX_TEST(verify_td_io_write, guest_io_write)
+{
+ struct ucall uc;
+ u32 port_data;
+
+ vcpu_run(vcpu);
+ TEST_ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_IO);
+ TEST_ASSERT_EQ(vcpu->run->io.port, TEST_IO_PORT);
+ TEST_ASSERT_EQ(vcpu->run->io.size, sizeof(TEST_IO_DATA_VALUE));
+ TEST_ASSERT_EQ(vcpu->run->io.direction, TDVMCALL_IO_WRITE);
+ port_data = *(u32 *)((void *)vcpu->run + vcpu->run->io.data_offset);
+ TEST_ASSERT_EQ(port_data, TEST_IO_DATA_VALUE);
+
+ vcpu_run(vcpu);
+ TEST_ASSERT_EQ(get_ucall(vcpu, &uc), UCALL_DONE);
+}
+
int main(int argc, char **argv)
{
TEST_REQUIRE(is_tdx_supported());
--
2.56.1.915.g69d5ec7298-goog