KVM: TDX: Exit to userspace for GetTdVmCallInfo
authorBinbin Wu <binbin.wu@linux.intel.com>
Tue, 10 Jun 2025 02:14:21 +0000 (10:14 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 20 Jun 2025 17:55:47 +0000 (13:55 -0400)
Exit to userspace for TDG.VP.VMCALL<GetTdVmCallInfo> via KVM_EXIT_TDX,
to allow userspace to provide information about the support of
TDVMCALLs when r12 is 1 for the TDVMCALLs beyond the GHCI base API.

GHCI spec defines the GHCI base TDVMCALLs: <GetTdVmCallInfo>, <MapGPA>,
<ReportFatalError>, <Instruction.CPUID>, <#VE.RequestMMIO>,
<Instruction.HLT>, <Instruction.IO>, <Instruction.RDMSR> and
<Instruction.WRMSR>. They must be supported by VMM to support TDX guests.

For GetTdVmCallInfo
- When leaf (r12) to enumerate TDVMCALL functionality is set to 0,
  successful execution indicates all GHCI base TDVMCALLs listed above are
  supported.

  Update the KVM TDX document with the set of the GHCI base APIs.

- When leaf (r12) to enumerate TDVMCALL functionality is set to 1, it
  indicates the TDX guest is querying the supported TDVMCALLs beyond
  the GHCI base TDVMCALLs.
  Exit to userspace to let userspace set the TDVMCALL sub-function bit(s)
  accordingly to the leaf outputs.  KVM could set the TDVMCALL bit(s)
  supported by itself when the TDVMCALLs don't need support from userspace
  after returning from userspace and before entering guest. Currently, no
  such TDVMCALLs implemented, KVM just sets the values returned from
  userspace.

Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Binbin Wu <binbin.wu@linux.intel.com>
[Adjust userspace API. - Paolo]
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 115ec3c2b641533c4f6e71510427d890313ffce2..9abf93ee5f65adf01af5ecc21560821eb6253356 100644 (file)
@@ -7191,6 +7191,11 @@ The valid value for 'flags' is:
                                        u64 gpa;
                                        u64 size;
                                } get_quote;
+                               struct {
+                                       u64 ret;
+                                       u64 leaf;
+                                       u64 r11, r12, r13, r14;
+                               } get_tdvmcall_info;
                        };
                } tdx;
 
@@ -7216,6 +7221,11 @@ queued successfully, the TDX guest can poll the status field in the
 shared-memory area to check whether the Quote generation is completed or
 not. When completed, the generated Quote is returned via the same buffer.
 
+* ``TDVMCALL_GET_TD_VM_CALL_INFO``: the guest has requested the support
+status of TDVMCALLs.  The output values for the given leaf should be
+placed in fields from ``r11`` to ``r14`` of the ``get_tdvmcall_info``
+field of the union.
+
 KVM may add support for more values in the future that may cause a userspace
 exit, even without calls to ``KVM_ENABLE_CAP`` or similar.  In this case,
 it will enter with output fields already valid; in the common case, the
index b619a3478983ab6602ac81420c36c6a5d23ea81e..1ad20c273f3b86f12bf8f7c777b1a62a55c10eb0 100644 (file)
@@ -1451,18 +1451,53 @@ error:
        return 1;
 }
 
+static int tdx_complete_get_td_vm_call_info(struct kvm_vcpu *vcpu)
+{
+       struct vcpu_tdx *tdx = to_tdx(vcpu);
+
+       tdvmcall_set_return_code(vcpu, vcpu->run->tdx.get_tdvmcall_info.ret);
+
+       /*
+        * For now, there is no TDVMCALL beyond GHCI base API supported by KVM
+        * directly without the support from userspace, just set the value
+        * returned from userspace.
+        */
+       tdx->vp_enter_args.r11 = vcpu->run->tdx.get_tdvmcall_info.r11;
+       tdx->vp_enter_args.r12 = vcpu->run->tdx.get_tdvmcall_info.r12;
+       tdx->vp_enter_args.r13 = vcpu->run->tdx.get_tdvmcall_info.r13;
+       tdx->vp_enter_args.r14 = vcpu->run->tdx.get_tdvmcall_info.r14;
+
+       return 1;
+}
+
 static int tdx_get_td_vm_call_info(struct kvm_vcpu *vcpu)
 {
        struct vcpu_tdx *tdx = to_tdx(vcpu);
 
-       if (tdx->vp_enter_args.r12)
-               tdvmcall_set_return_code(vcpu, TDVMCALL_STATUS_INVALID_OPERAND);
-       else {
+       switch (tdx->vp_enter_args.r12) {
+       case 0:
                tdx->vp_enter_args.r11 = 0;
+               tdx->vp_enter_args.r12 = 0;
                tdx->vp_enter_args.r13 = 0;
                tdx->vp_enter_args.r14 = 0;
+               tdvmcall_set_return_code(vcpu, TDVMCALL_STATUS_SUCCESS);
+               return 1;
+       case 1:
+               vcpu->run->tdx.get_tdvmcall_info.leaf = tdx->vp_enter_args.r12;
+               vcpu->run->exit_reason = KVM_EXIT_TDX;
+               vcpu->run->tdx.flags = 0;
+               vcpu->run->tdx.nr = TDVMCALL_GET_TD_VM_CALL_INFO;
+               vcpu->run->tdx.get_tdvmcall_info.ret = TDVMCALL_STATUS_SUCCESS;
+               vcpu->run->tdx.get_tdvmcall_info.r11 = 0;
+               vcpu->run->tdx.get_tdvmcall_info.r12 = 0;
+               vcpu->run->tdx.get_tdvmcall_info.r13 = 0;
+               vcpu->run->tdx.get_tdvmcall_info.r14 = 0;
+               vcpu->arch.complete_userspace_io = tdx_complete_get_td_vm_call_info;
+               return 0;
+       default:
+               tdvmcall_set_return_code(vcpu, TDVMCALL_STATUS_INVALID_OPERAND);
+               return 1;
        }
-       return 1;
 }
 
 static int tdx_complete_simple(struct kvm_vcpu *vcpu)
index e23e7286ad1a98a60a637bf2b5f22ec734b792bd..37891580d05d061a8c96e62d3bce8547e2fdda8a 100644 (file)
@@ -462,6 +462,11 @@ struct kvm_run {
                                        __u64 gpa;
                                        __u64 size;
                                } get_quote;
+                               struct {
+                                       __u64 ret;
+                                       __u64 leaf;
+                                       __u64 r11, r12, r13, r14;
+                               } get_tdvmcall_info;
                        };
                } tdx;
                /* Fix the size of the union. */