KVM: TDX: Handle TDG.VP.VMCALL<ReportFatalError>
authorBinbin Wu <binbin.wu@linux.intel.com>
Sat, 22 Feb 2025 01:42:23 +0000 (09:42 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 14 Mar 2025 18:20:55 +0000 (14:20 -0400)
Convert TDG.VP.VMCALL<ReportFatalError> to KVM_EXIT_SYSTEM_EVENT with
a new type KVM_SYSTEM_EVENT_TDX_FATAL and forward it to userspace for
handling.

TD guest can use TDG.VP.VMCALL<ReportFatalError> to report the fatal
error it has experienced.  This hypercall is special because TD guest
is requesting a termination with the error information, KVM needs to
forward the hypercall to userspace anyway, KVM doesn't do parsing or
conversion, it just dumps the 16 general-purpose registers to userspace
and let userspace decide what to do.

Signed-off-by: Binbin Wu <binbin.wu@linux.intel.com>
Message-ID: <20250222014225.897298-8-binbin.wu@linux.intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Documentation/virt/kvm/api.rst
arch/x86/kvm/vmx/tdx.c
include/uapi/linux/kvm.h

index 2b52eb77e29cba146b64489cfb1a267e7bb9b597..8dd3151fb9e47ddd44d90012fe3f74a3817a8728 100644 (file)
@@ -6823,6 +6823,7 @@ should put the acknowledged interrupt vector into the 'epr' field.
   #define KVM_SYSTEM_EVENT_WAKEUP         4
   #define KVM_SYSTEM_EVENT_SUSPEND        5
   #define KVM_SYSTEM_EVENT_SEV_TERM       6
+  #define KVM_SYSTEM_EVENT_TDX_FATAL      7
                        __u32 type;
                         __u32 ndata;
                         __u64 data[16];
@@ -6849,6 +6850,11 @@ Valid values for 'type' are:
    reset/shutdown of the VM.
  - KVM_SYSTEM_EVENT_SEV_TERM -- an AMD SEV guest requested termination.
    The guest physical address of the guest's GHCB is stored in `data[0]`.
+ - KVM_SYSTEM_EVENT_TDX_FATAL -- a TDX guest reported a fatal error state.
+   KVM doesn't do any parsing or conversion, it just dumps 16 general-purpose
+   registers to userspace, in ascending order of the 4-bit indices for x86-64
+   general-purpose registers in instruction encoding, as defined in the Intel
+   SDM.
  - KVM_SYSTEM_EVENT_WAKEUP -- the exiting vCPU is in a suspended state and
    KVM has recognized a wakeup event. Userspace may honor this event by
    marking the exiting vCPU as runnable, or deny it and call KVM_RUN again.
index d7a6c41737ce756050359aa3cf269c9d0564aa9e..5f6c29d8418099855f518b9fd927e198af7d5cba 100644 (file)
@@ -1104,11 +1104,39 @@ error:
        return 1;
 }
 
+static int tdx_report_fatal_error(struct kvm_vcpu *vcpu)
+{
+       struct vcpu_tdx *tdx = to_tdx(vcpu);
+       u64 *regs = vcpu->run->system_event.data;
+       u64 *module_regs = &tdx->vp_enter_args.r8;
+       int index = VCPU_REGS_RAX;
+
+       vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
+       vcpu->run->system_event.type = KVM_SYSTEM_EVENT_TDX_FATAL;
+       vcpu->run->system_event.ndata = 16;
+
+       /* Dump 16 general-purpose registers to userspace in ascending order. */
+       regs[index++] = tdx->vp_enter_ret;
+       regs[index++] = tdx->vp_enter_args.rcx;
+       regs[index++] = tdx->vp_enter_args.rdx;
+       regs[index++] = tdx->vp_enter_args.rbx;
+       regs[index++] = 0;
+       regs[index++] = 0;
+       regs[index++] = tdx->vp_enter_args.rsi;
+       regs[index] = tdx->vp_enter_args.rdi;
+       for (index = 0; index < 8; index++)
+               regs[VCPU_REGS_R8 + index] = module_regs[index];
+
+       return 0;
+}
+
 static int handle_tdvmcall(struct kvm_vcpu *vcpu)
 {
        switch (tdvmcall_leaf(vcpu)) {
        case TDVMCALL_MAP_GPA:
                return tdx_map_gpa(vcpu);
+       case TDVMCALL_REPORT_FATAL_ERROR:
+               return tdx_report_fatal_error(vcpu);
        default:
                break;
        }
index 45e6d8fca9b9969622dc15a6af924221782ac2e0..937400350317f50680d28be057d53128bdb364c3 100644 (file)
@@ -375,6 +375,7 @@ struct kvm_run {
 #define KVM_SYSTEM_EVENT_WAKEUP         4
 #define KVM_SYSTEM_EVENT_SUSPEND        5
 #define KVM_SYSTEM_EVENT_SEV_TERM       6
+#define KVM_SYSTEM_EVENT_TDX_FATAL      7
                        __u32 type;
                        __u32 ndata;
                        union {