[RFC PATCH v6 096/104] KVM: TDX: Handle TDX PV report fatal error hypercall

From: isaku . yamahata
Date: Thu May 05 2022 - 14:21:25 EST


From: Isaku Yamahata <isaku.yamahata@xxxxxxxxx>

Wire up TDX PV report fatal error hypercall to KVM_SYSTEM_EVENT_CRASH KVM
exit event.

Signed-off-by: Isaku Yamahata <isaku.yamahata@xxxxxxxxx>
---
arch/x86/kvm/vmx/tdx.c | 21 +++++++++++++++++++++
include/uapi/linux/kvm.h | 1 +
2 files changed, 22 insertions(+)

diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 1518a8c310d6..ee83539d5228 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -1202,6 +1202,25 @@ static int tdx_emulate_wrmsr(struct kvm_vcpu *vcpu)
return 1;
}

+static int tdx_report_fatal_error(struct kvm_vcpu *vcpu)
+{
+ /*
+ * Exit to userspace device model for teardown.
+ * Because guest TD is already panicing, returning an error to guerst TD
+ * doesn't make sense. No argument check is done.
+ */
+
+ vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
+ vcpu->run->system_event.type =
+ KVM_SYSTEM_EVENT_TDX | KVM_SYSTEM_EVENT_NDATA_VALID;
+ vcpu->run->system_event.ndata = 3;
+ vcpu->run->system_event.data[0] = TDG_VP_VMCALL_REPORT_FATAL_ERROR;
+ vcpu->run->system_event.data[1] = tdvmcall_a0_read(vcpu);
+ vcpu->run->system_event.data[2] = tdvmcall_a1_read(vcpu);
+
+ return 0;
+}
+
static int handle_tdvmcall(struct kvm_vcpu *vcpu)
{
if (tdvmcall_exit_type(vcpu))
@@ -1220,6 +1239,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)
return tdx_emulate_rdmsr(vcpu);
case EXIT_REASON_MSR_WRITE:
return tdx_emulate_wrmsr(vcpu);
+ case TDG_VP_VMCALL_REPORT_FATAL_ERROR:
+ return tdx_report_fatal_error(vcpu);
default:
break;
}
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 9a3fd7b41fc5..df1b89ffdac6 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -445,6 +445,7 @@ struct kvm_run {
#define KVM_SYSTEM_EVENT_RESET 2
#define KVM_SYSTEM_EVENT_CRASH 3
#define KVM_SYSTEM_EVENT_SEV_TERM 4
+#define KVM_SYSTEM_EVENT_TDX 5
#define KVM_SYSTEM_EVENT_NDATA_VALID (1u << 31)
__u32 type;
__u32 ndata;
--
2.25.1