[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20220830222000.709028-6-sagis@google.com>
Date: Tue, 30 Aug 2022 22:19:48 +0000
From: Sagi Shahar <sagis@...gle.com>
To: linux-kselftest@...r.kernel.org
Cc: Paolo Bonzini <pbonzini@...hat.com>,
Sean Christopherson <seanjc@...gle.com>,
Isaku Yamahata <isaku.yamahata@...el.com>,
Sagi Shahar <sagis@...gle.com>,
Erdem Aktas <erdemaktas@...gle.com>,
Ryan Afranji <afranji@...gle.com>,
Roger Wang <runanwang@...gle.com>,
Shuah Khan <shuah@...nel.org>,
Andrew Jones <drjones@...hat.com>,
Marc Zyngier <maz@...nel.org>, Ben Gardon <bgardon@...gle.com>,
Jim Mattson <jmattson@...gle.com>,
David Matlack <dmatlack@...gle.com>,
Peter Xu <peterx@...hat.com>, Oliver Upton <oupton@...gle.com>,
Ricardo Koller <ricarkol@...gle.com>,
Yang Zhong <yang.zhong@...el.com>,
Wei Wang <wei.w.wang@...el.com>,
Xiaoyao Li <xiaoyao.li@...el.com>,
Peter Gonda <pgonda@...gle.com>, Marc Orr <marcorr@...gle.com>,
Emanuele Giuseppe Esposito <eesposit@...hat.com>,
Christian Borntraeger <borntraeger@...ibm.com>,
Eric Auger <eric.auger@...hat.com>,
Yanan Wang <wangyanan55@...wei.com>,
Aaron Lewis <aaronlewis@...gle.com>,
Vitaly Kuznetsov <vkuznets@...hat.com>,
Peter Shier <pshier@...gle.com>,
Axel Rasmussen <axelrasmussen@...gle.com>,
Zhenzhong Duan <zhenzhong.duan@...el.com>,
"Maciej S . Szmigiero" <maciej.szmigiero@...cle.com>,
Like Xu <like.xu@...ux.intel.com>,
linux-kernel@...r.kernel.org, kvm@...r.kernel.org
Subject: [RFC PATCH v2 05/17] KVM: selftest: Adding test case for TDX port IO
From: Erdem Aktas <erdemaktas@...gle.com>
Verifies TDVMCALL<INSTRUCTION.IO> READ and WRITE operations.
Signed-off-by: Erdem Aktas <erdemaktas@...gle.com>
Signed-off-by: Sagi Shahar <sagis@...gle.com>
---
tools/testing/selftests/kvm/lib/x86_64/tdx.h | 1 +
.../selftests/kvm/x86_64/tdx_vm_tests.c | 108 ++++++++++++++++++
2 files changed, 109 insertions(+)
diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx.h b/tools/testing/selftests/kvm/lib/x86_64/tdx.h
index 351ece3e80e2..c78dba1af14f 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/tdx.h
+++ b/tools/testing/selftests/kvm/lib/x86_64/tdx.h
@@ -55,6 +55,7 @@
#define TDX_INSTRUCTION_IO 30
#define TDX_SUCCESS_PORT 0x30
+#define TDX_TEST_PORT 0x31
#define TDX_IO_READ 0
#define TDX_IO_WRITE 1
diff --git a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c
index 1db5400ca5ef..a93629cfd13f 100644
--- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c
+++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c
@@ -27,6 +27,32 @@
(VCPU)->run->exit_reason, exit_reason_str((VCPU)->run->exit_reason), \
(VCPU)->run->io.port, (VCPU)->run->io.size, (VCPU)->run->io.direction))
+#define CHECK_IO(VCPU, PORT, SIZE, DIR) \
+ do { \
+ TEST_ASSERT((VCPU)->run->exit_reason == KVM_EXIT_IO, \
+ "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n", \
+ (VCPU)->run->exit_reason, \
+ exit_reason_str((VCPU)->run->exit_reason)); \
+ \
+ TEST_ASSERT(((VCPU)->run->exit_reason == KVM_EXIT_IO) && \
+ ((VCPU)->run->io.port == (PORT)) && \
+ ((VCPU)->run->io.size == (SIZE)) && \
+ ((VCPU)->run->io.direction == (DIR)), \
+ "Got an unexpected IO exit values: %u (%s) %d %d %d\n", \
+ (VCPU)->run->exit_reason, \
+ exit_reason_str((VCPU)->run->exit_reason), \
+ (VCPU)->run->io.port, (VCPU)->run->io.size, \
+ (VCPU)->run->io.direction); \
+ } while (0)
+
+#define CHECK_GUEST_FAILURE(VCPU) \
+ do { \
+ if ((VCPU)->run->exit_reason == KVM_EXIT_SYSTEM_EVENT) \
+ TEST_FAIL("Guest reported error. error code: %lld (0x%llx)\n", \
+ (VCPU)->run->system_event.data[1], \
+ (VCPU)->run->system_event.data[1]); \
+ } while (0)
+
/*
* There might be multiple tests we are running and if one test fails, it will
* prevent the subsequent tests to run due to how tests are failing with
@@ -145,6 +171,87 @@ void verify_report_fatal_error(void)
printf("\t ... PASSED\n");
}
+/*
+ * Verifies IO functionality by writing a |value| to a predefined port.
+ * Verifies that the read value is |value| + 1 from the same port.
+ * If all the tests are passed then write a value to port TDX_TEST_PORT
+ */
+TDX_GUEST_FUNCTION(guest_io_exit)
+{
+ uint64_t data_out, data_in, delta;
+ uint64_t ret;
+
+ data_out = 0xAB;
+
+ ret = tdvmcall_io(TDX_TEST_PORT, 1, TDX_IO_WRITE, &data_out);
+ if (ret)
+ tdvmcall_fatal(ret);
+
+ ret = tdvmcall_io(TDX_TEST_PORT, 1, TDX_IO_READ, &data_in);
+ if (ret)
+ tdvmcall_fatal(ret);
+
+ delta = data_in - data_out;
+ if (delta != 1)
+ tdvmcall_fatal(ret);
+
+ tdvmcall_success();
+}
+
+void verify_td_ioexit(void)
+{
+ struct kvm_vcpu *vcpu;
+ uint32_t port_data;
+ struct kvm_vm *vm;
+
+ printf("Verifying TD IO Exit:\n");
+ /* Create a TD VM with no memory.*/
+ vm = vm_create_tdx();
+
+ /* Allocate TD guest memory and initialize the TD.*/
+ initialize_td(vm);
+
+ /* Initialize the TD vcpu and copy the test code to the guest memory.*/
+ vcpu = vm_vcpu_add_tdx(vm, 0);
+
+ /* Setup and initialize VM memory */
+ prepare_source_image(vm, guest_io_exit,
+ TDX_FUNCTION_SIZE(guest_io_exit), 0);
+ finalize_td_memory(vm);
+
+ /* Wait for guest to do a IO write */
+ vcpu_run(vcpu);
+ CHECK_GUEST_FAILURE(vcpu);
+ CHECK_IO(vcpu, TDX_TEST_PORT, 1, TDX_IO_WRITE);
+ port_data = *(uint8_t *)((void *)vcpu->run + vcpu->run->io.data_offset);
+
+ printf("\t ... IO WRITE: OK\n");
+
+ /*
+ * Wait for the guest to do a IO read. Provide the previos written data
+ * + 1 back to the guest
+ */
+ vcpu_run(vcpu);
+ CHECK_GUEST_FAILURE(vcpu);
+ CHECK_IO(vcpu, TDX_TEST_PORT, 1, TDX_IO_READ);
+ *(uint8_t *)((void *)vcpu->run + vcpu->run->io.data_offset) = port_data + 1;
+
+ printf("\t ... IO READ: OK\n");
+
+ /*
+ * Wait for the guest to complete execution successfully. The read
+ * value is checked within the guest.
+ */
+ vcpu_run(vcpu);
+ CHECK_GUEST_FAILURE(vcpu);
+ CHECK_GUEST_COMPLETION(vcpu);
+
+ printf("\t ... IO verify read/write values: OK\n");
+ kvm_vm_free(vm);
+ printf("\t ... PASSED\n");
+}
+
+
int main(int argc, char **argv)
{
if (!is_tdx_enabled()) {
@@ -154,6 +261,7 @@ int main(int argc, char **argv)
run_in_new_process(&verify_td_lifecycle);
run_in_new_process(&verify_report_fatal_error);
+ run_in_new_process(&verify_td_ioexit);
return 0;
}
--
2.37.2.789.g6183377224-goog
Powered by blists - more mailing lists