[PATCH 4/6] KVM: x86: Fill kvm_pio_request if and only if KVM is exiting to userspace

From: Sean Christopherson
Date: Thu Oct 21 2021 - 18:41:18 EST


Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
arch/x86/kvm/x86.c | 89 +++++++++++++++++++++++-----------------------
1 file changed, 45 insertions(+), 44 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index a126b1129348..a20a790ce586 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7044,19 +7044,17 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
return emulator_write_emulated(ctxt, addr, new, bytes, exception);
}

-static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
+static int kernel_pio(struct kvm_vcpu *vcpu, int size, unsigned short port,
+ void *data, unsigned int count, bool in)
{
unsigned int i;
int r;

- for (i = 0; i < vcpu->arch.pio.count; i++) {
- if (vcpu->arch.pio.in)
- r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
- vcpu->arch.pio.size, pd);
+ for (i = 0; i < count; i++) {
+ if (in)
+ r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, port, size, data);
else
- r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
- vcpu->arch.pio.port, vcpu->arch.pio.size,
- pd);
+ r = kvm_io_bus_write(vcpu, KVM_PIO_BUS, port, size, data);
if (r) {
/*
* The port doesn't change on subsequent iterations and
@@ -7065,24 +7063,33 @@ static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
WARN_ON_ONCE(i);
return r;
}
- pd += vcpu->arch.pio.size;
+ data += size;
}
return 0;
}

static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
- unsigned short port, void *val,
+ unsigned short port, void *data,
unsigned int count, bool in)
{
+ if (!kernel_pio(vcpu, port, size, data, count, in))
+ return 1;
+
+ /*
+ * I/O was not handled in kernel, forward the operation to userespace.
+ * Snapshot the port, size, etc... in kernel memory as some callers,
+ * e.g. "fast" port I/O and SEV-ES, don't flow through the emulator and
+ * will have lost the original information when KVM regains control.
+ * The info stored in the run page can't be trusted as userspace has
+ * write access to the run page.
+ */
vcpu->arch.pio.port = port;
vcpu->arch.pio.in = in;
- vcpu->arch.pio.count = count;
+ vcpu->arch.pio.count = count;
vcpu->arch.pio.size = size;

- if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
- vcpu->arch.pio.count = 0;
- return 1;
- }
+ if (!in)
+ memcpy(vcpu->arch.pio_data, data, size * count);

vcpu->run->exit_reason = KVM_EXIT_IO;
vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
@@ -7090,30 +7097,27 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
vcpu->run->io.count = count;
vcpu->run->io.port = port;
-
return 0;
}

static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
- unsigned short port, void *val, unsigned int count)
+ unsigned short port, void *data, unsigned int count)
{
- int ret;
-
- if (vcpu->arch.pio.count)
- goto data_avail;
-
- memset(vcpu->arch.pio_data, 0, size * count);
-
- 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);
+ if (vcpu->arch.pio.count) {
+ /*
+ * Complete port I/O when re-emulating the instruction after
+ * userspace has provided the requested data.
+ *
+ * FIXME: this will copy garbage if count > vcpu->arch.pio.count.
+ */
vcpu->arch.pio.count = 0;
- return 1;
+ memcpy(data, vcpu->arch.pio_data, size * count);
+ } else if (!emulator_pio_in_out(vcpu, size, port, data, count, true)) {
+ return 0;
}

- return 0;
+ trace_kvm_pio(KVM_PIO_IN, port, size, count, data);
+ return 1;
}

static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
@@ -7125,19 +7129,18 @@ static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
}

static int emulator_pio_out(struct kvm_vcpu *vcpu, int size,
- unsigned short port, const void *val,
+ unsigned short port, void *val,
unsigned int count)
{
- memcpy(vcpu->arch.pio_data, val, size * count);
- trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
- return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
+ trace_kvm_pio(KVM_PIO_OUT, port, size, count, val);
+ return emulator_pio_in_out(vcpu, size, port, val, count, false);
}

static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
int size, unsigned short port,
- const void *val, unsigned int count)
+ const void *data, unsigned int count)
{
- return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count);
+ return emulator_pio_out(emul_to_vcpu(ctxt), size, port, (void *)data, count);
}

static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
@@ -12509,14 +12512,12 @@ static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size,

ret = emulator_pio_in_emulated(vcpu->arch.emulate_ctxt, size, port,
data, count);
- if (ret) {
- vcpu->arch.pio.count = 0;
- } else {
- vcpu->arch.guest_ins_data = data;
- vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins;
- }
+ if (ret)
+ return ret;

- return ret;
+ vcpu->arch.guest_ins_data = data;
+ vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins;
+ return 0;
}

int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
--
2.33.0.1079.g6e70778dc9-goog


--kTbyx1pw6IySRUfp
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
filename="0005-KVM-x86-Stop-being-clever-and-use-a-completion-handl.patch"