Skip to content

Commit 3b27de2

Browse files
committed
KVM: x86: split the two parts of emulator_pio_in
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: [email protected] Fixes: 7ed9abf ("KVM: SVM: Support string IO operations for an SEV-ES guest") Signed-off-by: Paolo Bonzini <[email protected]>
1 parent ea724ea commit 3b27de2

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

arch/x86/kvm/x86.c

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6906,7 +6906,7 @@ static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
69066906
}
69076907

69086908
static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
6909-
unsigned short port, void *val,
6909+
unsigned short port,
69106910
unsigned int count, bool in)
69116911
{
69126912
vcpu->arch.pio.port = port;
@@ -6927,26 +6927,38 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
69276927
return 0;
69286928
}
69296929

6930-
static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
6931-
unsigned short port, void *val, unsigned int count)
6930+
static int __emulator_pio_in(struct kvm_vcpu *vcpu, int size,
6931+
unsigned short port, unsigned int count)
69326932
{
6933-
int ret;
6933+
WARN_ON(vcpu->arch.pio.count);
6934+
memset(vcpu->arch.pio_data, 0, size * count);
6935+
return emulator_pio_in_out(vcpu, size, port, count, true);
6936+
}
69346937

6935-
if (vcpu->arch.pio.count)
6936-
goto data_avail;
6938+
static void complete_emulator_pio_in(struct kvm_vcpu *vcpu, int size,
6939+
unsigned short port, void *val)
6940+
{
6941+
memcpy(val, vcpu->arch.pio_data, size * vcpu->arch.pio.count);
6942+
trace_kvm_pio(KVM_PIO_IN, port, size, vcpu->arch.pio.count, vcpu->arch.pio_data);
6943+
vcpu->arch.pio.count = 0;
6944+
}
69376945

6938-
memset(vcpu->arch.pio_data, 0, size * count);
6946+
static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,
6947+
unsigned short port, void *val, unsigned int count)
6948+
{
6949+
if (vcpu->arch.pio.count) {
6950+
/* Complete previous iteration. */
6951+
} else {
6952+
int r = __emulator_pio_in(vcpu, size, port, count);
6953+
if (!r)
6954+
return r;
69396955

6940-
ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
6941-
if (ret) {
6942-
data_avail:
6943-
memcpy(val, vcpu->arch.pio_data, size * count);
6944-
trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
6945-
vcpu->arch.pio.count = 0;
6946-
return 1;
6956+
/* Results already available, fall through. */
69476957
}
69486958

6949-
return 0;
6959+
WARN_ON(count != vcpu->arch.pio.count);
6960+
complete_emulator_pio_in(vcpu, size, port, val);
6961+
return 1;
69506962
}
69516963

69526964
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,
69656977

69666978
memcpy(vcpu->arch.pio_data, val, size * count);
69676979
trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
6968-
ret = emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
6980+
ret = emulator_pio_in_out(vcpu, size, port, count, false);
69696981
if (ret)
69706982
vcpu->arch.pio.count = 0;
69716983

69726984
return ret;
6973-
69746985
}
69756986

69766987
static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,

0 commit comments

Comments
 (0)