diff mbox series

[v17,093/116] KVM: TDX: Handle TDX PV port io hypercall

Message ID d8b438b6be78b4dbeea5c2a0ecaddfc06bfea43c.1699368322.git.isaku.yamahata@intel.com (mailing list archive)
State New, archived
Headers show
Series KVM TDX basic feature support | expand

Commit Message

Isaku Yamahata Nov. 7, 2023, 2:56 p.m. UTC
From: Isaku Yamahata <isaku.yamahata@intel.com>

Wire up TDX PV port IO hypercall to the KVM backend function.

Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/vmx/tdx.c | 57 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

Comments

Sagi Shahar Dec. 9, 2023, 12:26 a.m. UTC | #1
On Tue, Nov 7, 2023 at 6:58 AM <isaku.yamahata@intel.com> wrote:
>
> From: Isaku Yamahata <isaku.yamahata@intel.com>
>
> Wire up TDX PV port IO hypercall to the KVM backend function.
>
> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  arch/x86/kvm/vmx/tdx.c | 57 ++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 57 insertions(+)
>
> diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
> index 4e48989d364f..d4b09255ad32 100644
> --- a/arch/x86/kvm/vmx/tdx.c
> +++ b/arch/x86/kvm/vmx/tdx.c
> @@ -1140,6 +1140,61 @@ static int tdx_emulate_hlt(struct kvm_vcpu *vcpu)
>         return kvm_emulate_halt_noskip(vcpu);
>  }
>
> +static int tdx_complete_pio_in(struct kvm_vcpu *vcpu)
> +{
> +       struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
> +       unsigned long val = 0;
> +       int ret;
> +
> +       WARN_ON_ONCE(vcpu->arch.pio.count != 1);
> +
> +       ret = ctxt->ops->pio_in_emulated(ctxt, vcpu->arch.pio.size,
> +                                        vcpu->arch.pio.port, &val, 1);
> +       WARN_ON_ONCE(!ret);
> +
> +       tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_SUCCESS);
> +       tdvmcall_set_return_val(vcpu, val);
> +
> +       return 1;
> +}
> +
> +static int tdx_emulate_io(struct kvm_vcpu *vcpu)
> +{
> +       struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
> +       unsigned long val = 0;
> +       unsigned int port;
> +       int size, ret;
> +       bool write;
> +
> +       ++vcpu->stat.io_exits;
> +
> +       size = tdvmcall_a0_read(vcpu);
> +       write = tdvmcall_a1_read(vcpu);
> +       port = tdvmcall_a2_read(vcpu);
> +
> +       if (size != 1 && size != 2 && size != 4) {
> +               tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_INVALID_OPERAND);
> +               return 1;
> +       }
> +
> +       if (write) {
> +               val = tdvmcall_a3_read(vcpu);
> +               ret = ctxt->ops->pio_out_emulated(ctxt, size, port, &val, 1);
> +
> +               /* No need for a complete_userspace_io callback. */
> +               vcpu->arch.pio.count = 0;
> +       } else {
> +               ret = ctxt->ops->pio_in_emulated(ctxt, size, port, &val, 1);
> +               if (!ret)
> +                       vcpu->arch.complete_userspace_io = tdx_complete_pio_in;
> +               else
> +                       tdvmcall_set_return_val(vcpu, val);
> +       }
> +       if (ret)
> +               tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_SUCCESS);

In the case of a write operation that needs to exit to userspace,
pio_out_emulated is going to return 0 which means that the return code
is never set by tdx_emulate_io.
The reason this code is working now is because r10 is already set to 0
by the guest as part of the call to tdvmcall and never gets written by
the host.

> +       return ret;
> +}
> +
>  static int handle_tdvmcall(struct kvm_vcpu *vcpu)
>  {
>         if (tdvmcall_exit_type(vcpu))
> @@ -1150,6 +1205,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu)
>                 return tdx_emulate_cpuid(vcpu);
>         case EXIT_REASON_HLT:
>                 return tdx_emulate_hlt(vcpu);
> +       case EXIT_REASON_IO_INSTRUCTION:
> +               return tdx_emulate_io(vcpu);
>         default:
>                 break;
>         }
> --
> 2.25.1
>
diff mbox series

Patch

diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 4e48989d364f..d4b09255ad32 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -1140,6 +1140,61 @@  static int tdx_emulate_hlt(struct kvm_vcpu *vcpu)
 	return kvm_emulate_halt_noskip(vcpu);
 }
 
+static int tdx_complete_pio_in(struct kvm_vcpu *vcpu)
+{
+	struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
+	unsigned long val = 0;
+	int ret;
+
+	WARN_ON_ONCE(vcpu->arch.pio.count != 1);
+
+	ret = ctxt->ops->pio_in_emulated(ctxt, vcpu->arch.pio.size,
+					 vcpu->arch.pio.port, &val, 1);
+	WARN_ON_ONCE(!ret);
+
+	tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_SUCCESS);
+	tdvmcall_set_return_val(vcpu, val);
+
+	return 1;
+}
+
+static int tdx_emulate_io(struct kvm_vcpu *vcpu)
+{
+	struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
+	unsigned long val = 0;
+	unsigned int port;
+	int size, ret;
+	bool write;
+
+	++vcpu->stat.io_exits;
+
+	size = tdvmcall_a0_read(vcpu);
+	write = tdvmcall_a1_read(vcpu);
+	port = tdvmcall_a2_read(vcpu);
+
+	if (size != 1 && size != 2 && size != 4) {
+		tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_INVALID_OPERAND);
+		return 1;
+	}
+
+	if (write) {
+		val = tdvmcall_a3_read(vcpu);
+		ret = ctxt->ops->pio_out_emulated(ctxt, size, port, &val, 1);
+
+		/* No need for a complete_userspace_io callback. */
+		vcpu->arch.pio.count = 0;
+	} else {
+		ret = ctxt->ops->pio_in_emulated(ctxt, size, port, &val, 1);
+		if (!ret)
+			vcpu->arch.complete_userspace_io = tdx_complete_pio_in;
+		else
+			tdvmcall_set_return_val(vcpu, val);
+	}
+	if (ret)
+		tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_SUCCESS);
+	return ret;
+}
+
 static int handle_tdvmcall(struct kvm_vcpu *vcpu)
 {
 	if (tdvmcall_exit_type(vcpu))
@@ -1150,6 +1205,8 @@  static int handle_tdvmcall(struct kvm_vcpu *vcpu)
 		return tdx_emulate_cpuid(vcpu);
 	case EXIT_REASON_HLT:
 		return tdx_emulate_hlt(vcpu);
+	case EXIT_REASON_IO_INSTRUCTION:
+		return tdx_emulate_io(vcpu);
 	default:
 		break;
 	}