]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/commitdiff
KVM: x86: split the two parts of emulator_pio_in
authorPaolo Bonzini <pbonzini@redhat.com>
Wed, 13 Oct 2021 16:32:02 +0000 (12:32 -0400)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 22 Oct 2021 14:08:00 +0000 (10:08 -0400)
emulator_pio_in handles both the case where the data is pending in
vcpu->arch.pio.count, and the case where I/O has to be done via either
an in-kernel device or a userspace exit.  For SEV-ES we would like
to split these, to identify clearly the moment at which the
sev_pio_data is consumed.  To this end, create two different
functions: __emulator_pio_in fills in vcpu->arch.pio.count, while
complete_emulator_pio_in clears it and releases vcpu->arch.pio.data.

Because this patch has to be backported, things are left a bit messy.
kernel_pio() operates on vcpu->arch.pio, which leads to emulator_pio_in()
having with two calls to complete_emulator_pio_in().  It will be fixed
in the next release.

While at it, remove the unused void* val argument of emulator_pio_in_out.
The function currently hardcodes vcpu->arch.pio_data as the
source/destination buffer, which sucks but will be fixed after the more
severe SEV-ES buffer overflow.

No functional change intended.

Cc: stable@vger.kernel.org
Fixes: 7ed9abfe8e9f ("KVM: SVM: Support string IO operations for an SEV-ES guest")
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/x86.c

index 78ed0fe9fa1e9e12eac595d18c10db1e388803f7..c51ea81019e30a38164166ba91085046087b0e97 100644 (file)
@@ -6906,7 +6906,7 @@ static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
 }
 
 static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
-                              unsigned short port, void *val,
+                              unsigned short port,
                               unsigned int count, bool in)
 {
        vcpu->arch.pio.port = port;
@@ -6927,26 +6927,38 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
        return 0;
 }
 
-static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
-                          unsigned short port, void *val, unsigned int count)
+static int __emulator_pio_in(struct kvm_vcpu *vcpu, int size,
+                            unsigned short port, unsigned int count)
 {
-       int ret;
+       WARN_ON(vcpu->arch.pio.count);
+       memset(vcpu->arch.pio_data, 0, size * count);
+       return emulator_pio_in_out(vcpu, size, port, count, true);
+}
 
-       if (vcpu->arch.pio.count)
-               goto data_avail;
+static void complete_emulator_pio_in(struct kvm_vcpu *vcpu, int size,
+                                   unsigned short port, void *val)
+{
+       memcpy(val, vcpu->arch.pio_data, size * vcpu->arch.pio.count);
+       trace_kvm_pio(KVM_PIO_IN, port, size, vcpu->arch.pio.count, vcpu->arch.pio_data);
+       vcpu->arch.pio.count = 0;
+}
 
-       memset(vcpu->arch.pio_data, 0, size * count);
+static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
+                          unsigned short port, void *val, unsigned int count)
+{
+       if (vcpu->arch.pio.count) {
+               /* Complete previous iteration.  */
+       } else {
+               int r = __emulator_pio_in(vcpu, size, port, count);
+               if (!r)
+                       return r;
 
-       ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
-       if (ret) {
-data_avail:
-               memcpy(val, vcpu->arch.pio_data, size * count);
-               trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
-               vcpu->arch.pio.count = 0;
-               return 1;
+               /* Results already available, fall through.  */
        }
 
-       return 0;
+       WARN_ON(count != vcpu->arch.pio.count);
+       complete_emulator_pio_in(vcpu, size, port, val);
+       return 1;
 }
 
 static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
@@ -6965,12 +6977,11 @@ static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
 
        memcpy(vcpu->arch.pio_data, val, size * count);
        trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
-       ret = emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
+       ret = emulator_pio_in_out(vcpu, size, port, count, false);
        if (ret)
                 vcpu->arch.pio.count = 0;
 
         return ret;
-
 }
 
 static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,