diff mbox series

[RFC,v2,04/17] KVM: selftest: TDX: Add report_fatal_error test

Message ID 20220830222000.709028-5-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 checks report_fatal_error functionality.

Signed-off-by: Sagi Shahar <sagis@google.com>
---
 tools/testing/selftests/kvm/lib/x86_64/tdx.h  | 17 ++++++
 .../selftests/kvm/x86_64/tdx_vm_tests.c       | 55 +++++++++++++++++++
 2 files changed, 72 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 d5de52657112..351ece3e80e2 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_REPORT_FATAL_ERROR 0x10003
 #define TDX_INSTRUCTION_IO 30
 
 #define TDX_SUCCESS_PORT 0x30
@@ -212,6 +213,22 @@  static inline void tdvmcall_success(void)
 	tdvmcall_io(TDX_SUCCESS_PORT, /*size=*/4, TDX_IO_WRITE, &code);
 }
 
+/*
+ * Report an error to user space.
+ * data_gpa may point to an optional shared guest memory holding the error string.
+ * Return value from tdvmcall is ignored since execution is not expected to
+ * continue beyond this point.
+ */
+static inline void tdvmcall_fatal(uint64_t error_code)
+{
+	struct kvm_regs regs;
+
+	memset(&regs, 0, sizeof(regs));
+	regs.r11 = TDX_REPORT_FATAL_ERROR;
+	regs.r12 = error_code;
+	regs.rcx = 0x1C00;
+	tdcall(&regs);
+}
 
 #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 590e45aa7570..1db5400ca5ef 100644
--- a/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c
+++ b/tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c
@@ -91,6 +91,60 @@  void verify_td_lifecycle(void)
 	printf("\t ... PASSED\n");
 }
 
+/*
+ * Verifies TDX_REPORT_FATAL_ERROR functionality.
+ */
+TDX_GUEST_FUNCTION(guest_code_report_fatal_error)
+{
+	uint64_t err;
+	/* Note: err should follow the GHCI spec definition:
+	 * bits 31:0 should be set to 0.
+	 * bits 62:32 are used for TD-specific extended error code.
+	 * bit 63 is used to mark additional information in shared memory.
+	 */
+	err = 0x0BAAAAAD00000000;
+
+	if (err)
+		tdvmcall_fatal(err);
+
+	tdvmcall_success();
+}
+
+void verify_report_fatal_error(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+
+	printf("Verifying report_fatal_error:\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_report_fatal_error,
+			     TDX_FUNCTION_SIZE(guest_code_report_fatal_error),
+			     0);
+	finalize_td_memory(vm);
+
+	vcpu_run(vcpu);
+	ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_SYSTEM_EVENT);
+	ASSERT_EQ(vcpu->run->system_event.ndata, 3);
+	ASSERT_EQ(vcpu->run->system_event.data[0], TDX_REPORT_FATAL_ERROR);
+	ASSERT_EQ(vcpu->run->system_event.data[1], 0x0BAAAAAD00000000);
+	ASSERT_EQ(vcpu->run->system_event.data[2], 0);
+
+	vcpu_run(vcpu);
+	CHECK_GUEST_COMPLETION(vcpu);
+
+	kvm_vm_free(vm);
+	printf("\t ... PASSED\n");
+}
+
 int main(int argc, char **argv)
 {
 	if (!is_tdx_enabled()) {
@@ -99,6 +153,7 @@  int main(int argc, char **argv)
 	}
 
 	run_in_new_process(&verify_td_lifecycle);
+	run_in_new_process(&verify_report_fatal_error);
 
 	return 0;
 }