diff mbox series

[RFC,v2,07/17] KVM: selftest: TDX: Add basic get_td_vmcall_info test

Message ID 20220830222000.709028-8-sagis@google.com (mailing list archive)
State New
Headers show
Series TDX KVM selftests | expand

Commit Message

Sagi Shahar Aug. 30, 2022, 10:19 p.m. UTC
The test calls get_td_vmcall_info from the guest and verifies the
expected returned values.

Signed-off-by: Sagi Shahar <sagis@google.com>
---
 tools/testing/selftests/kvm/lib/x86_64/tdx.h  |  43 +++++++
 .../selftests/kvm/x86_64/tdx_vm_tests.c       | 107 ++++++++++++++++++
 2 files changed, 150 insertions(+)
diff mbox series

Patch

diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx.h b/tools/testing/selftests/kvm/lib/x86_64/tdx.h
index a28d15417d3e..39b000118e26 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/tdx.h
+++ b/tools/testing/selftests/kvm/lib/x86_64/tdx.h
@@ -51,6 +51,7 @@ 
 #define _PAGE_RW            (1UL<<1)       /* writeable */
 #define _PAGE_PS            (1UL<<7)       /* page size bit*/
 
+#define TDX_GET_TD_VM_CALL_INFO 0x10000
 #define TDX_REPORT_FATAL_ERROR 0x10003
 #define TDX_INSTRUCTION_IO 30
 
@@ -232,6 +233,28 @@  static inline void tdvmcall_fatal(uint64_t error_code)
 	tdcall(&regs);
 }
 
+/*
+ * Get td vmcall info.
+ * Used to help request the host VMM enumerate which TDG.VP.VMCALLs are supported.
+ * Returns return in r10 code and leaf-specific output in r11-r14.
+ */
+static inline uint64_t tdvmcall_get_td_vmcall_info(uint64_t *r11, uint64_t *r12,
+						   uint64_t *r13, uint64_t *r14)
+{
+	struct kvm_regs regs;
+
+	memset(&regs, 0, sizeof(regs));
+	regs.r11 = TDX_GET_TD_VM_CALL_INFO;
+	regs.r12 = 0;
+	regs.rcx = 0x1C00;
+	tdcall(&regs);
+	*r11 = regs.r11;
+	*r12 = regs.r12;
+	*r13 = regs.r13;
+	*r14 = regs.r14;
+	return regs.r10;
+}
+
 /*
  * Reports a 32 bit value from the guest to user space using a TDVM IO call.
  * Data is reported on port TDX_DATA_REPORT_PORT.
@@ -244,6 +267,26 @@  static inline uint64_t tdvm_report_to_user_space(uint32_t data)
 	return tdvmcall_io(TDX_DATA_REPORT_PORT, /*size=*/4, TDX_IO_WRITE, &data_64);
 }
 
+/*
+ * Reports a 64 bit value from the guest to user space using a TDVM IO call.
+ * Data is reported on port TDX_DATA_REPORT_PORT.
+ * Data is sent to host in 2 calls. LSB is sent (and needs to be read) first.
+ */
+static inline uint64_t tdvm_report_64bit_to_user_space(uint64_t data)
+{
+	uint64_t err;
+	uint64_t data_lo = data & 0xFFFFFFFF;
+	uint64_t data_hi = (data >> 32) & 0xFFFFFFFF;
+
+	err = tdvmcall_io(TDX_DATA_REPORT_PORT, /*size=*/4, TDX_IO_WRITE,
+			  &data_lo);
+	if (err)
+		return err;
+
+	return tdvmcall_io(TDX_DATA_REPORT_PORT, /*size=*/4, TDX_IO_WRITE,
+			   &data_hi);
+}
+
 #define TDX_FUNCTION_SIZE(name) ((uint64_t)&__stop_sec_ ## name -\
 			   (uint64_t)&__start_sec_ ## name) \
 
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 3f51f936ea5a..cf8260db1f5b 100644
--- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c
+++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c
@@ -53,6 +53,25 @@ 
 				  (VCPU)->run->system_event.data[1]);			\
 	} while (0)
 
+static uint64_t read_64bit_from_guest(struct kvm_vcpu *vcpu, uint64_t port)
+{
+	uint32_t lo, hi;
+	uint64_t res;
+
+	CHECK_IO(vcpu, port, 4, TDX_IO_WRITE);
+	lo = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset);
+
+	vcpu_run(vcpu);
+
+	CHECK_IO(vcpu, port, 4, TDX_IO_WRITE);
+	hi = *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset);
+
+	res = hi;
+	res = (res << 32) | lo;
+	return res;
+}
+
+
 /*
  * 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
@@ -383,6 +402,93 @@  void verify_td_cpuid(void)
 	printf("\t ... PASSED\n");
 }
 
+/*
+ * Verifies get_td_vmcall_info functionality.
+ */
+TDX_GUEST_FUNCTION(guest_code_get_td_vmcall_info)
+{
+	uint64_t err;
+	uint64_t r11, r12, r13, r14;
+
+	err = tdvmcall_get_td_vmcall_info(&r11, &r12, &r13, &r14);
+	if (err)
+		tdvmcall_fatal(err);
+
+	err = tdvm_report_64bit_to_user_space(r11);
+	if (err)
+		tdvmcall_fatal(err);
+
+	err = tdvm_report_64bit_to_user_space(r12);
+	if (err)
+		tdvmcall_fatal(err);
+
+	err = tdvm_report_64bit_to_user_space(r13);
+	if (err)
+		tdvmcall_fatal(err);
+
+	err = tdvm_report_64bit_to_user_space(r14);
+	if (err)
+		tdvmcall_fatal(err);
+
+	tdvmcall_success();
+}
+
+void verify_get_td_vmcall_info(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	uint64_t r11, r12, r13, r14;
+
+	printf("Verifying TD get vmcall info:\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_code_get_td_vmcall_info,
+			     TDX_FUNCTION_SIZE(guest_code_get_td_vmcall_info),
+			     0);
+	finalize_td_memory(vm);
+
+	/* Wait for guest to report r11 value */
+	vcpu_run(vcpu);
+	CHECK_GUEST_FAILURE(vcpu);
+	r11 = read_64bit_from_guest(vcpu, TDX_DATA_REPORT_PORT);
+
+	/* Wait for guest to report r12 value */
+	vcpu_run(vcpu);
+	CHECK_GUEST_FAILURE(vcpu);
+	r12 = read_64bit_from_guest(vcpu, TDX_DATA_REPORT_PORT);
+
+	/* Wait for guest to report r13 value */
+	vcpu_run(vcpu);
+	CHECK_GUEST_FAILURE(vcpu);
+	r13 = read_64bit_from_guest(vcpu, TDX_DATA_REPORT_PORT);
+
+	/* Wait for guest to report r14 value */
+	vcpu_run(vcpu);
+	CHECK_GUEST_FAILURE(vcpu);
+	r14 = read_64bit_from_guest(vcpu, TDX_DATA_REPORT_PORT);
+
+	ASSERT_EQ(r11, 0);
+	ASSERT_EQ(r12, 0);
+	ASSERT_EQ(r13, 0);
+	ASSERT_EQ(r14, 0);
+
+	/* Wait for guest to complete execution */
+	vcpu_run(vcpu);
+	CHECK_GUEST_FAILURE(vcpu);
+	CHECK_GUEST_COMPLETION(vcpu);
+
+	kvm_vm_free(vm);
+	printf("\t ... PASSED\n");
+}
+
 int main(int argc, char **argv)
 {
 	if (!is_tdx_enabled()) {
@@ -394,6 +500,7 @@  int main(int argc, char **argv)
 	run_in_new_process(&verify_report_fatal_error);
 	run_in_new_process(&verify_td_ioexit);
 	run_in_new_process(&verify_td_cpuid);
+	run_in_new_process(&verify_get_td_vmcall_info);
 
 	return 0;
 }