KVM: SEV-ES: fixes for string I/O emulation
Subsystems (4)
Paolo Bonzini
maintainercover KVM: SEV-ES: fixes for string I/O emulation
This series, namely patches 1 and 8, fix two bugs in string I/O emulation for SEV-ES:
- first, the length is completely off for "rep ins" and "rep outs" operation of size > 1. After setup_vmgexit_scratch, svm->ghcb_sa_len is in bytes, but kvm_sev_es_string_io expects the number of PIO operations.
- second, the size of the GHCB buffer can exceed the size of vcpu->arch.pio_data. If that happens, we need to go over the GHCB buffer in multiple passes.
The second bug was reported by Felix Wilhelm. The first was found by me by code inspection; on one hand it seems *too* egregious so I'll be gladly proven wrong on this, on the other hand... I know I'm bad at code review, but not _that_ bad.
Patches 2 to 7 are a bunch of cleanups to emulator_pio_in and emulator_pio_in_out, so that the final SEV code is a little easier to reason on. Just a little, no big promises.
Tested by booting a SEV-ES guest and with "regular" kvm-unit-tests. For SEV-ES I also bounded the limit to 12 bytes, and checked in the resulting trace that both the read and write paths were hit when booting a guest.
Paolo
Paolo Bonzini (8):
KVM: SEV-ES: fix length of string I/O
KVM: SEV-ES: rename guest_ins_data to sev_pio_data
KVM: x86: leave vcpu->arch.pio.count alone in emulator_pio_in_out
KVM: SEV-ES: clean up kvm_sev_es_ins/outs
KVM: x86: split the two parts of emulator_pio_in
KVM: x86: remove unnecessary arguments from complete_emulator_pio_in
KVM: SEV-ES: keep INS functions together
KVM: SEV-ES: go over the sev_pio_data buffer in multiple passes if
needed
arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/svm/sev.c | 2 +- arch/x86/kvm/x86.c | 136 +++++++++++++++++++++----------- 3 files changed, 95 insertions(+), 46 deletions(-)
Signature · 1 line
2.27.0
Paolo Bonzini
maintainerPATCH 1/8 KVM: SEV-ES: fix length of string I/O
The size of the data in the scratch buffer is not divided by the size of each port I/O operation, so vcpu->arch.pio.count ends up being larger than it should be by a factor of size.
--- arch/x86/kvm/svm/sev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +1 -1
@@ -2583,7 +2583,7 @@ int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)return -EINVAL ;return kvm_sev_es_string_io (&svm->vcpu, size, port, - svm->ghcb_sa, svm->ghcb_sa_len, in); + svm->ghcb_sa, svm->ghcb_sa_len / size, in); }void sev_es_init_vmcb (struct vcpu_svm *svm)
Signature · 1 line
2.27.0
Re: [PATCH 1/8] KVM: SEV-ES: fix length of string I/O
Quoted 7 lines · Paolo Bonzini
The size of the data in the scratch buffer is not divided by the size of each port I/O operation, so
vcpu->arch.pio.countends up being larger than it should be by a factor of size.Fixes 7ed9abfe8e9f ("KVM: SVM: Support string IO operations for an SEV-ES guest")Signed-off-by Paolo Bonzini <[email protected]>
Ugh, can't believe I did that...
Quoted 18 lines
--- arch/x86/kvm/svm/sev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/svm/sev.c +1
-1@@ -2583,7 +2583,7 @@ int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)return -EINVAL ;return kvm_sev_es_string_io (&svm->vcpu, size, port, - svm->ghcb_sa, svm->ghcb_sa_len, in); + svm->ghcb_sa, svm->ghcb_sa_len / size, in); }void sev_es_init_vmcb (struct vcpu_svm *svm)
Re: [PATCH 1/8] KVM: SEV-ES: fix length of string I/O
In reply to Paolo BonziniQuoted 16 lines · Paolo Bonzini
The size of the data in the scratch buffer is not divided by the size of each port I/O operation, so
vcpu->arch.pio.countends up being larger than it should be by a factor of size.Fixes 7ed9abfe8e9f ("KVM: SVM: Support string IO operations for an SEV-ES guest")Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/svm/sev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/svm/sev.c +0
-0
arch/x86/kvm/svm/sev.c int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in) · line 2589
Quoted 8 lines
arch/x86/kvm/svm/sev.c +1
-1@@ -2583,7 +2583,7 @@ int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)return -EINVAL ;return kvm_sev_es_string_io (&svm->vcpu, size, port, - svm->ghcb_sa, svm->ghcb_sa_len, in); + svm->ghcb_sa, svm->ghcb_sa_len / size, in); }void sev_es_init_vmcb (struct vcpu_svm *svm)
This ends in kvm_sev_es_ins/outs and both indeed expect count of operations which they pass to emulator_pio_{out|in}_emulated
Best regards,
Maxim Levitsky
Re: [PATCH 1/8] KVM: SEV-ES: fix length of string I/O
In reply to Paolo BonziniQuoted 26 lines · Paolo Bonzini
The size of the data in the scratch buffer is not divided by the size of each port I/O operation, so
vcpu->arch.pio.countends up being larger than it should be by a factor of size.Fixes 7ed9abfe8e9f ("KVM: SVM: Support string IO operations for an SEV-ES guest")Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/svm/sev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/svm/sev.c +1
-2@@ -2583,7 +2583,7 @@ int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)return -EINVAL ;return kvm_sev_es_string_io (&svm->vcpu, size, port, - svm->ghcb_sa, svm->ghcb_sa_len, in); + svm->ghcb_sa, svm->ghcb_sa_len / size, in); }void sev_es_init_vmcb (struct vcpu_svm *svm) --
Quoted 3 lines
2.27.0
I could be missing something, but I'm pretty sure that this is wrong. The GHCB spec says that exit_info_2 is the rep count. Not the string length.
For example, given a rep outsw instruction, with ECX set to 8, the rep count written into SW_EXITINFO2 should be eight x86 words (i.e., 16 bytes) and the IO size should be one x86 word (i.e., 2 bytes). In other words, the code was correct before this patch. This patch is incorrectly dividing the rep count by the IO size, causing the string IO to be truncated.
Paolo Bonzini
maintainerRe: [PATCH 1/8] KVM: SEV-ES: fix length of string I/O
Quoted 10 lines · Marc Orr
I could be missing something, but I'm pretty sure that this is wrong. The GHCB spec says that
exit_info_2is therepcount. Not the string length.For example, given a
rep outswinstruction, withECXset to8, the rep count written intoSW_EXITINFO2should be eight x86 words (i.e., 16 bytes) and the IO size should be one x86 word (i.e., 2 bytes). In other words, the code was correct before this patch. This patch is incorrectly dividing the rep count by the IO size, causing the string IO to be truncated.
Then what's wrong is _also_ the call to setup_vmgexit_scratch, because that one definitely expects bytes:
scratch_va = kzalloc(len, GFP_KERNEL_ACCOUNT);
Paolo
Paolo Bonzini
maintainerPATCH 2/8 KVM: SEV-ES: rename guest_ins_data to sev_pio_data
In reply to Paolo BonziniSince we will be using this for OUTS emulation as well, change the name to something that refers to any kind of PIO. Also spell out what it is used for, namely SEV-ES.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/x86.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -702,7 +702,7 @@ struct kvm_vcpu_arch {struct kvm_pio_request pio;void *pio_data; -void *guest_ins_data; +void *sev_pio_data;u8 event_exit_inst_len;
arch/x86/kvm/x86.c +2 -2
@@ -12369,7 +12369,7 @@ EXPORT_SYMBOL_GPL(kvm_sev_es_mmio_read);static int complete_sev_es_emulated_ins (struct kvm_vcpu *vcpu) { -memcpy (vcpu->arch.guest_ins_data, vcpu->arch.pio_data, +memcpy (vcpu->arch.sev_pio_data, vcpu->arch.pio_data, vcpu->arch.pio.count * vcpu->arch.pio.size); vcpu->arch.pio.count =0 ;
@@ -12401,7 +12401,7 @@ static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size,if (ret) { vcpu->arch.pio.count =0 ; }else { - vcpu->arch.guest_ins_data = data; + vcpu->arch.sev_pio_data = data; vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; }
Signature · 1 line
2.27.0
Re: [PATCH 2/8] KVM: SEV-ES: rename guest_ins_data to sev_pio_data
Quoted 41 lines · Paolo Bonzini
Since we will be using this for OUTS emulation as well, change the name to something that refers to any kind of PIO. Also spell out what it is used for, namely SEV-ES.
No functional change intended.
Fixes 7ed9abfe8e9f ("KVM: SVM: Support string IO operations for an SEV-ES guest")Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/x86.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm_host.h +1
-1@@ -702,7 +702,7 @@ struct kvm_vcpu_arch {struct kvm_pio_request pio;void *pio_data; -void *guest_ins_data; +void *sev_pio_data;u8 event_exit_inst_len;arch/x86/kvm/x86.c +1
-1@@ -12369,7 +12369,7 @@ EXPORT_SYMBOL_GPL(kvm_sev_es_mmio_read);static int complete_sev_es_emulated_ins (struct kvm_vcpu *vcpu) { -memcpy (vcpu->arch.guest_ins_data, vcpu->arch.pio_data, +memcpy (vcpu->arch.sev_pio_data, vcpu->arch.pio_data, vcpu->arch.pio.count * vcpu->arch.pio.size); vcpu->arch.pio.count =0 ;
arch/x86/kvm/x86.c static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size, · line 12407
Quoted 8 lines
arch/x86/kvm/x86.c +1
-1@@ -12401,7 +12401,7 @@ static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size,if (ret) { vcpu->arch.pio.count =0 ; }else { - vcpu->arch.guest_ins_data = data; + vcpu->arch.sev_pio_data = data; vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; }
It might be worth to mention here why we will soon need this field for the outs emulation:
INS reads the data from the userspace (or in-kernel) PIO emulation which is provided in vcpu->arch.pio_data which is then copied to GHCB, but for OUTS, the data is pushed from GHCB to userspace/in-kernel PIO emulation, so there is no need to do anything SEV specific
But if the data is pushed via outs spans more that one page, next few patches will split it, so there will be a need to save the data pointer.
Best regards,
Maxim Levitsky
Paolo Bonzini
maintainerPATCH 3/8 KVM: x86: leave vcpu->arch.pio.count alone in emulator_pio_in_out
In reply to Paolo BonziniCurrently emulator_pio_in clears vcpu->arch.pio.count twice if emulator_pio_in_out performs kernel PIO. Move the clear into emulator_pio_out where it is actually necessary.
No functional change intended.
--- arch/x86/kvm/x86.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
arch/x86/kvm/x86.c +9 -4
@@ -6914,10 +6914,8 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size, vcpu->arch.pio.count = count; vcpu->arch.pio.size = size; -if (!kernel_pio (vcpu, vcpu->arch.pio_data)) { - vcpu->arch.pio.count =0 ; +if (!kernel_pio (vcpu, vcpu->arch.pio_data))return 1 ; - } vcpu->run->exit_reason =KVM_EXIT_IO ; vcpu->run->io.direction = in ?KVM_EXIT_IO_IN :KVM_EXIT_IO_OUT ;
@@ -6963,9 +6961,16 @@ static int emulator_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port, const void *val, unsigned int count) { + int ret; +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); + ret =emulator_pio_in_out (vcpu, size, port, (void *)val, count, false); +if (ret) + vcpu->arch.pio.count =0 ; + +return ret; + } static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
Signature · 1 line
2.27.0
Re: [PATCH 3/8] KVM: x86: leave vcpu->arch.pio.count alone in emulator_pio_in_out
Quoted 37 lines · Paolo Bonzini
Currently
emulator_pio_inclearsvcpu->arch.pio.counttwice ifemulator_pio_in_outperforms kernel PIO. Move the clear intoemulator_pio_outwhere it is actually necessary.No functional change intended.
Fixes 7ed9abfe8e9f ("KVM: SVM: Support string IO operations for an SEV-ES guest")Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/x86.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)arch/x86/kvm/x86.c +3
-3@@ -6914,10 +6914,8 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size, vcpu->arch.pio.count = count; vcpu->arch.pio.size = size; -if (!kernel_pio (vcpu, vcpu->arch.pio_data)) { - vcpu->arch.pio.count =0 ; +if (!kernel_pio (vcpu, vcpu->arch.pio_data))return 1 ; - } vcpu->run->exit_reason =KVM_EXIT_IO ; vcpu->run->io.direction = in ?KVM_EXIT_IO_IN :KVM_EXIT_IO_OUT ;@@ -6963,9 +6961,16 @@ static int emulator_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port, const void *val, unsigned int count) { + int ret; +memcpy (vcpu->arch.pio_data, val, size * count);trace_kvm_pio (KVM_PIO_OUT , port, size, count, vcpu->arch.pio_data);
arch/x86/kvm/x86.c static int emulator_pio_out(struct kvm_vcpu *vcpu, int size, · line 6976
Quoted 10 lines
arch/x86/kvm/x86.c +6
-1-return emulator_pio_in_out (vcpu, size, port, (void *)val, count, false); + ret =emulator_pio_in_out (vcpu, size, port, (void *)val, count, false); +if (ret) + vcpu->arch.pio.count =0 ; + +return ret; + } static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
Makes sense, now that both emulator_pio_in and emulator_pio_out clear the arch.pio.count once.
Best regards,
Maxim Levitsky
Paolo Bonzini
maintainerPATCH 4/8 KVM: SEV-ES: clean up kvm_sev_es_ins/outs
In reply to Paolo BonziniRemove the data argument and pull setting vcpu->arch.sev_pio_data into the caller; remove unnecessary clearing of vcpu->arch.pio.count when emulation is done by the kernel.
No functional change intended.
--- arch/x86/kvm/x86.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-)
arch/x86/kvm/x86.c +15 -16
@@ -12382,34 +12382,32 @@ static int complete_sev_es_emulated_ins(struct kvm_vcpu *vcpu) }static int kvm_sev_es_outs (struct kvm_vcpu *vcpu,unsigned int size, -unsigned int port,void *data,unsigned int count) +unsigned int port,unsigned int count) { -int ret; +int ret =emulator_pio_out (vcpu, size, port, + vcpu->arch.sev_pio_data, count); - ret =emulator_pio_out_emulated (vcpu->arch.emulate_ctxt, size, port, - data, count); -if (ret) +if (ret) { +/* Emulation done by the kernel. */ return ret; + } vcpu->arch.pio.count =0 ; -return 0 ; }static int kvm_sev_es_ins (struct kvm_vcpu *vcpu,unsigned int size, -unsigned int port,void *data,unsigned int count) +unsigned int port,unsigned int count) { -int ret; +int ret =emulator_pio_in (vcpu, size, port, + vcpu->arch.sev_pio_data, count); - ret =emulator_pio_in_emulated (vcpu->arch.emulate_ctxt, size, port, - data, count);if (ret) { - vcpu->arch.pio.count =0 ; - }else { - vcpu->arch.sev_pio_data = data; - vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; +/* Emulation done by the kernel. */ +return ret; } + vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins;return 0 ; }
@@ -12417,8 +12415,9 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, unsigned int port, void *data, unsigned int count, int in) { - return in ? kvm_sev_es_ins(vcpu, size, port, data, count) - : kvm_sev_es_outs(vcpu, size, port, data, count); + vcpu->arch.sev_pio_data = data; +return in ?kvm_sev_es_ins (vcpu, size, port, count) + :kvm_sev_es_outs (vcpu, size, port, count); }EXPORT_SYMBOL_GPL (kvm_sev_es_string_io);
Signature · 1 line
2.27.0
Re: [PATCH 4/8] KVM: SEV-ES: clean up kvm_sev_es_ins/outs
Quoted 3 lines · Paolo Bonzini
Remove the data argument and pull setting
vcpu->arch.sev_pio_datainto the caller; remove unnecessary clearing ofvcpu->arch.pio.countwhen emulation is done by the kernel.
You forgot to mention that you inlined the call to emulator_pio_out_emulated/emulator_pio_in_emulated.
Quoted 20 lines
No functional change intended.
Fixes 7ed9abfe8e9f ("KVM: SVM: Support string IO operations for an SEV-ES guest")Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/x86.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-)arch/x86/kvm/x86.c +1
-1@@ -12382,34 +12382,32 @@ static int complete_sev_es_emulated_ins(struct kvm_vcpu *vcpu) }static int kvm_sev_es_outs (struct kvm_vcpu *vcpu,unsigned int size, -unsigned int port,void *data,unsigned int count) +unsigned int port,unsigned int count)
arch/x86/kvm/x86.c static int complete_sev_es_emulated_ins(struct kvm_vcpu *vcpu) · line 12391
Quoted 10 lines
arch/x86/kvm/x86.c +4
-4{ -int ret; +int ret =emulator_pio_out (vcpu, size, port, + vcpu->arch.sev_pio_data, count); - ret =emulator_pio_out_emulated (vcpu->arch.emulate_ctxt, size, port, - data, count); -if (ret) +if (ret) { +/* Emulation done by the kernel. */
^^ This is a very good comment to have here!
arch/x86/kvm/x86.c static int complete_sev_es_emulated_ins(struct kvm_vcpu *vcpu) · line 12395
Quoted 4 lines
arch/x86/kvm/x86.c +1
-0return ret; + } vcpu->arch.pio.count =0 ;
^^^
I wonder what the rules are for clearing vcpu->arch.pio.count for userspace PIO vm exits. Looks like complete_fast_pio_out clears it, but otherwise the only other place that clears it in this case is x86_emulate_instruction when it restarts the instuction. Do I miss something?
Quoted 29 lines
arch/x86/kvm/x86.c +6
-9-return 0 ; }static int kvm_sev_es_ins (struct kvm_vcpu *vcpu,unsigned int size, -unsigned int port,void *data,unsigned int count) +unsigned int port,unsigned int count) { -int ret; +int ret =emulator_pio_in (vcpu, size, port, + vcpu->arch.sev_pio_data, count); - ret =emulator_pio_in_emulated (vcpu->arch.emulate_ctxt, size, port, - data, count);if (ret) { - vcpu->arch.pio.count =0 ; - }else { - vcpu->arch.sev_pio_data = data; - vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; +/* Emulation done by the kernel. */ +return ret; } + vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins;return 0 ; }@@ -12417,8 +12415,9 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, unsigned int port, void *data, unsigned int count,
arch/x86/kvm/x86.c int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, · line 12423
Quoted 10 lines
arch/x86/kvm/x86.c +3
-2int in) { -return in ?kvm_sev_es_ins (vcpu, size, port, data, count) - :kvm_sev_es_outs (vcpu, size, port, data, count); + vcpu->arch.sev_pio_data = data; +return in ?kvm_sev_es_ins (vcpu, size, port, count) + :kvm_sev_es_outs (vcpu, size, port, count); }EXPORT_SYMBOL_GPL (kvm_sev_es_string_io);
Looks OK to me, I might have missed something though.
Best regards,
Maxim Levitsky
Paolo Bonzini
maintainerRe: [PATCH 4/8] KVM: SEV-ES: clean up kvm_sev_es_ins/outs
Quoted 7 lines · Maxim Levitsky
Quoted 2 lines
vcpu->arch.pio.count = 0;^^^
I wonder what the rules are for clearingvcpu->arch.pio.countfor userspace PIO vm exits. Looks likecomplete_fast_pio_outclears it, but otherwise the only other place that clears it in this case isx86_emulate_instructionwhen it restarts the instuction. Do I miss something?
For IN, it is cleared by the completion callback.
For OUT, it can be cleared either by the completion callback or before calling it, because the completion callback will not need it. I would like to standardize towards clearing it in the callback for out, too, even if sometimes it's unnecessary to have a callback in the first place; this is what patch 8 does for example. This way vcpu->arch.pio.count > 0 tells you whether the other fields have a recent value.
Paolo
Paolo Bonzini
maintainerPATCH 5/8 KVM: x86: split the two parts of emulator_pio_in
In reply to Paolo Bonziniemulator_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.
While at it, remove the void* argument also from emulator_pio_in_out.
No functional change intended.
--- arch/x86/kvm/x86.c | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-)
arch/x86/kvm/x86.c +23 -19
@@ -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,31 @@ 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; - -if (vcpu->arch.pio.count) - goto data_avail; - +WARN_ON (vcpu->arch.pio.count);memset (vcpu->arch.pio_data,0 , size * count); +return emulator_pio_in_out (vcpu, size, port, count, true); +} - 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 ; - } +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 ; +} -return 0 ; +static int emulator_pio_in (struct kvm_vcpu *vcpu,int size, +unsigned short port,void *val,unsigned int count) +{ +if (!vcpu->arch.pio.count && !__emulator_pio_in (vcpu, size, port, count)) +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 +6970,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,
Signature · 1 line
2.27.0
Re: [PATCH 5/8] KVM: x86: split the two parts of emulator_pio_in
Quoted 9 lines · Paolo Bonzini
emulator_pio_inhandles both the case where the data is pending invcpu->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 thesev_pio_datais consumed. To this end, create two different
functions:__emulator_pio_infills invcpu->arch.pio.count, whilecomplete_emulator_pio_inclears it and releasesvcpu->arch.pio.data.While at it, remove the void* argument also from
emulator_pio_in_out.
s/remove the void* argument/remove the unused 'void* val' argument/ maybe?
Quoted 54 lines
No functional change intended.
Fixes 7ed9abfe8e9f ("KVM: SVM: Support string IO operations for an SEV-ES guest")Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/x86.c | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-)arch/x86/kvm/x86.c +10
-16@@ -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,31 @@ 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; - -if (vcpu->arch.pio.count) - goto data_avail; - +WARN_ON (vcpu->arch.pio.count);memset (vcpu->arch.pio_data,0 , size * count); +return emulator_pio_in_out (vcpu, size, port, count, true); +} - 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 ; - } +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);
arch/x86/kvm/x86.c static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size, · line 6950
Quoted 10 lines
arch/x86/kvm/x86.c +8
-1+trace_kvm_pio (KVM_PIO_IN , port, size, vcpu->arch.pio.count, vcpu->arch.pio_data); + vcpu->arch.pio.count =0 ; +} -return 0 ; +static int emulator_pio_in (struct kvm_vcpu *vcpu,int size, +unsigned short port,void *val,unsigned int count) +{ +if (!vcpu->arch.pio.count && !__emulator_pio_in (vcpu, size, port, count)) +return 0 ;
^^ maybe I would add a comment here about the fact that kernel completed the emulation when returing here.
Quoted 11 lines
arch/x86/kvm/x86.c +4
-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 +6970,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);
arch/x86/kvm/x86.c static int emulator_pio_out(struct kvm_vcpu *vcpu, int size, · line 6980
Quoted 10 lines
arch/x86/kvm/x86.c +1
-2- 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,
Best regards,
Maxim Levitsky
Paolo Bonzini
maintainerPATCH 6/8 KVM: x86: remove unnecessary arguments from complete_emulator_pio_in
In reply to Paolo Bonzinicomplete_emulator_pio_in can expect that vcpu->arch.pio has been filled in, and therefore does not need the size and count arguments. This makes things nicer when the function is called directly from a complete_userspace_io callback.
No functional change intended.
--- arch/x86/kvm/x86.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/x86.c +5 -4
@@ -6935,11 +6935,12 @@ static int __emulator_pio_in(struct kvm_vcpu *vcpu, int size,return emulator_pio_in_out (vcpu, size, port, count, true); } -static void complete_emulator_pio_in (struct kvm_vcpu *vcpu,int size, -unsigned short port,void *val) +static void complete_emulator_pio_in (struct kvm_vcpu *vcpu,void *val) { +int size = vcpu->arch.pio.size;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); +trace_kvm_pio (KVM_PIO_IN , vcpu->arch.pio.port, size, + vcpu->arch.pio.count, vcpu->arch.pio_data); vcpu->arch.pio.count =0 ; }
@@ -6950,7 +6951,7 @@ static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,return 0 ;WARN_ON (count != vcpu->arch.pio.count); -complete_emulator_pio_in (vcpu, size, port, val); +complete_emulator_pio_in (vcpu, val);return 1 ; }
Signature · 1 line
2.27.0
Re: [PATCH 6/8] KVM: x86: remove unnecessary arguments from complete_emulator_pio_in
Quoted 35 lines · Paolo Bonzini
complete_emulator_pio_incan expect thatvcpu->arch.piohas been filled in, and therefore does not need the size and count arguments. This makes things nicer when the function is called directly from acomplete_userspace_iocallback.No functional change intended.
Fixes 7ed9abfe8e9f ("KVM: SVM: Support string IO operations for an SEV-ES guest")Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/x86.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)arch/x86/kvm/x86.c +4
-3@@ -6935,11 +6935,12 @@ static int __emulator_pio_in(struct kvm_vcpu *vcpu, int size,return emulator_pio_in_out (vcpu, size, port, count, true); } -static void complete_emulator_pio_in (struct kvm_vcpu *vcpu,int size, -unsigned short port,void *val) +static void complete_emulator_pio_in (struct kvm_vcpu *vcpu,void *val) { +int size = vcpu->arch.pio.size;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); +trace_kvm_pio (KVM_PIO_IN , vcpu->arch.pio.port, size, + vcpu->arch.pio.count, vcpu->arch.pio_data); vcpu->arch.pio.count =0 ; }
arch/x86/kvm/x86.c static int emulator_pio_in(struct kvm_vcpu *vcpu, int size, · line 6957
Quoted 8 lines
arch/x86/kvm/x86.c +1
-1@@ -6950,7 +6951,7 @@ static int emulator_pio_in(struct kvm_vcpu *vcpu, int size,return 0 ;WARN_ON (count != vcpu->arch.pio.count); -complete_emulator_pio_in (vcpu, size, port, val); +complete_emulator_pio_in (vcpu, val);return 1 ; }
Best regards,
Maxim Levitsky
Paolo Bonzini
maintainerPATCH 7/8 KVM: SEV-ES: keep INS functions together
In reply to Paolo BonziniMake the diff a little nicer when we actually get to fixing the bug. No functional change intended.
--- arch/x86/kvm/x86.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
arch/x86/kvm/x86.c +9 -9
@@ -12377,15 +12377,6 @@ int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes, }EXPORT_SYMBOL_GPL (kvm_sev_es_mmio_read); -static int complete_sev_es_emulated_ins (struct kvm_vcpu *vcpu) -{ -memcpy (vcpu->arch.sev_pio_data, vcpu->arch.pio_data, - vcpu->arch.pio.count * vcpu->arch.pio.size); - vcpu->arch.pio.count =0 ; - -return 1 ; -} -static int kvm_sev_es_outs (struct kvm_vcpu *vcpu,unsigned int size,unsigned int port,unsigned int count) {
@@ -12401,6 +12392,15 @@ static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, unsigned int size,return 0 ; } +static int complete_sev_es_emulated_ins (struct kvm_vcpu *vcpu) +{ +memcpy (vcpu->arch.sev_pio_data, vcpu->arch.pio_data, + vcpu->arch.pio.count * vcpu->arch.pio.size); + vcpu->arch.pio.count =0 ; + +return 1 ; +} +static int kvm_sev_es_ins (struct kvm_vcpu *vcpu,unsigned int size,unsigned int port,unsigned int count) {
Signature · 1 line
2.27.0
Re: [PATCH 7/8] KVM: SEV-ES: keep INS functions together
Quoted 36 lines · Paolo Bonzini
Make the diff a little nicer when we actually get to fixing the bug. No functional change intended.
Fixes 7ed9abfe8e9f ("KVM: SVM: Support string IO operations for an SEV-ES guest")Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/x86.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)arch/x86/kvm/x86.c +2
-9@@ -12377,15 +12377,6 @@ int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes, }EXPORT_SYMBOL_GPL (kvm_sev_es_mmio_read); -static int complete_sev_es_emulated_ins (struct kvm_vcpu *vcpu) -{ -memcpy (vcpu->arch.sev_pio_data, vcpu->arch.pio_data, - vcpu->arch.pio.count * vcpu->arch.pio.size); - vcpu->arch.pio.count =0 ; - -return 1 ; -} -static int kvm_sev_es_outs (struct kvm_vcpu *vcpu,unsigned int size,unsigned int port,unsigned int count) {@@ -12401,6 +12392,15 @@ static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, unsigned int size,return 0 ; } +static int complete_sev_es_emulated_ins (struct kvm_vcpu *vcpu) +{
arch/x86/kvm/x86.c static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, unsigned int size, · line 12406
Quoted 10 lines
arch/x86/kvm/x86.c +7
-0+memcpy (vcpu->arch.sev_pio_data, vcpu->arch.pio_data, + vcpu->arch.pio.count * vcpu->arch.pio.size); + vcpu->arch.pio.count =0 ; + +return 1 ; +} +static int kvm_sev_es_ins (struct kvm_vcpu *vcpu,unsigned int size,unsigned int port,unsigned int count) {
Best regards,
Maxim Levitsky
Paolo Bonzini
maintainerPATCH 8/8 KVM: SEV-ES: go over the sev_pio_data buffer in multiple passes if needed
In reply to Paolo BonziniThe PIO scratch buffer is larger than a single page, and therefore it is not possible to copy it in a single step to vcpu->arch/pio_data. Bound each call to emulator_pio_in/out to a single page; keep track of how many I/O operations are left in vcpu->arch.sev_pio_count, so that the operation can be restarted in the complete_userspace_io callback.
For OUT, this means that the previous kvm_sev_es_outs implementation becomes an iterator of the loop, and we can consume the sev_pio_data buffer before leaving to userspace.
For IN, instead, consuming the buffer and decreasing sev_pio_count is always done in the complete_userspace_io callback, because that is when the memcpy is done into sev_pio_data.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 73 +++++++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 17 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -703,6 +703,7 @@ struct kvm_vcpu_arch {struct kvm_pio_request pio;void *pio_data;void *sev_pio_data; +unsigned sev_pio_count;u8 event_exit_inst_len;
arch/x86/kvm/x86.c +56 -17
102 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [PATCH 8/8] KVM: SEV-ES: go over the sev_pio_data buffer in multiple passes if needed
Quoted 134 lines · Paolo Bonzini
The PIO scratch buffer is larger than a single page, and therefore it is not possible to copy it in a single step to vcpu->arch/pio_data. Bound each call to emulator_pio_in/out to a single page; keep track of how many I/O operations are left in
vcpu->arch.sev_pio_count, so that the operation can be restarted in thecomplete_userspace_iocallback.For OUT, this means that the previous
kvm_sev_es_outsimplementation becomes an iterator of the loop, and we can consume thesev_pio_databuffer before leaving to userspace.For IN, instead, consuming the buffer and decreasing
sev_pio_countis always done in thecomplete_userspace_iocallback, because that is when the memcpy is done intosev_pio_data.Fixes 7ed9abfe8e9f ("KVM: SVM: Support string IO operations for an SEV-ES guest")Reported-by Felix Wilhelm <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 73 +++++++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 17 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -703,6 +703,7 @@ struct kvm_vcpu_arch {struct kvm_pio_request pio;void *pio_data;void *sev_pio_data; +unsigned sev_pio_count;u8 event_exit_inst_len;arch/x86/kvm/x86.c +53
-15@@ -12378,38 +12378,76 @@ int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes,EXPORT_SYMBOL_GPL (kvm_sev_es_mmio_read);static int kvm_sev_es_outs (struct kvm_vcpu *vcpu,unsigned int size, -unsigned int port,unsigned int count) +unsigned int port); + +static int complete_sev_es_emulated_outs (struct kvm_vcpu *vcpu) { -int ret =emulator_pio_out (vcpu, size, port, - vcpu->arch.sev_pio_data, count); + vcpu->arch.pio.count =0 ; +if (vcpu->arch.sev_pio_count) +return kvm_sev_es_outs (vcpu, + vcpu->arch.pio.size, + vcpu->arch.pio.port); +return 1 ; +} + +static int kvm_sev_es_outs (struct kvm_vcpu *vcpu,unsigned int size, +unsigned int port) +{ +for (;;) { +unsigned int count = + min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); +int ret =emulator_pio_out (vcpu, size, port, vcpu->arch.sev_pio_data, count); + +/* memcpy done already by emulator_pio_out. */ + vcpu->arch.sev_pio_count -= count; + vcpu->arch.sev_pio_data += count * vcpu->arch.pio.size; +if (!ret) +break ; -if (ret) {/* Emulation done by the kernel. */ -return ret; + vcpu->arch.pio.count =0 ; +if (!vcpu->arch.sev_pio_count) +return 1 ; } - vcpu->arch.pio.count =0 ; + vcpu->arch.complete_userspace_io = complete_sev_es_emulated_outs;return 0 ; } -static int complete_sev_es_emulated_ins (struct kvm_vcpu *vcpu) +static int kvm_sev_es_ins (struct kvm_vcpu *vcpu,unsigned int size, +unsigned int port); + +static void __complete_sev_es_emulated_ins (struct kvm_vcpu *vcpu) { -memcpy (vcpu->arch.sev_pio_data, vcpu->arch.pio_data, - vcpu->arch.pio.count * vcpu->arch.pio.size); - vcpu->arch.pio.count =0 ; +unsigned count = vcpu->arch.pio.count; +complete_emulator_pio_in (vcpu, vcpu->arch.sev_pio_data); + vcpu->arch.sev_pio_count -= count; + vcpu->arch.sev_pio_data += count * vcpu->arch.pio.size; +} +static int complete_sev_es_emulated_ins (struct kvm_vcpu *vcpu) +{ +__complete_sev_es_emulated_ins (vcpu); +if (vcpu->arch.sev_pio_count) +return kvm_sev_es_ins (vcpu, + vcpu->arch.pio.size, + vcpu->arch.pio.port);return 1 ; }static int kvm_sev_es_ins (struct kvm_vcpu *vcpu,unsigned int size, -unsigned int port,unsigned int count) +unsigned int port) { -int ret =emulator_pio_in (vcpu, size, port, - vcpu->arch.sev_pio_data, count); +for (;;) { +unsigned int count = + min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); +if (!__emulator_pio_in (vcpu, size, port, count)) +break ; -if (ret) {/* Emulation done by the kernel. */ -return ret; +__complete_sev_es_emulated_ins (vcpu); +if (!vcpu->arch.sev_pio_count) +return 1 ; } vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins;@@ -12421,8 +12459,9 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, int in)
arch/x86/kvm/x86.c int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, · line 12467
Quoted 10 lines
arch/x86/kvm/x86.c +3
-2{ vcpu->arch.sev_pio_data = data; -return in ?kvm_sev_es_ins (vcpu, size, port, count) - :kvm_sev_es_outs (vcpu, size, port, count); + vcpu->arch.sev_pio_count = count; +return in ?kvm_sev_es_ins (vcpu, size, port) + :kvm_sev_es_outs (vcpu, size, port); }EXPORT_SYMBOL_GPL (kvm_sev_es_string_io);
I might have missed something, but it looks OK to me. i mostly checked how the code looks after applying this patch.
Note that I haven't run tested the patches as I don't currently test SEV-ES.
Best regards,
Maxim Levitsky
Paolo Bonzini
maintainerQuoted 8 lines · Paolo Bonzini
This series, namely patches 1 and 8, fix two bugs in string I/O emulation for SEV-ES:
- first, the length is completely off for "rep ins" and "rep outs" operation of size > 1. After
setup_vmgexit_scratch,svm->ghcb_sa_lenis in bytes, butkvm_sev_es_string_ioexpects the number of PIO operations.
Quoted 8 lines
- second, the size of the GHCB buffer can exceed the size of
vcpu->arch.pio_data. If that happens, we need to go over the GHCB buffer in multiple passes.The second bug was reported by Felix Wilhelm. The first was found by me by code inspection; on one hand it seems *too* egregious so I'll be gladly proven wrong on this, on the other hand... I know I'm bad at code review, but not _that_ bad.
Ping.
Paolo
Sean Christopherson
maintainerQuoted 9 lines · Paolo Bonzini
Quoted 8 lines · Paolo Bonzini
This series, namely patches 1 and 8, fix two bugs in string I/O emulation for SEV-ES:
- first, the length is completely off for "rep ins" and "rep outs" operation of size > 1. After
setup_vmgexit_scratch,svm->ghcb_sa_lenis in bytes, butkvm_sev_es_string_ioexpects the number of PIO operations.
Quoted 8 lines
Quoted 8 lines
- second, the size of the GHCB buffer can exceed the size of
vcpu->arch.pio_data. If that happens, we need to go over the GHCB buffer in multiple passes.The second bug was reported by Felix Wilhelm. The first was found by me by code inspection; on one hand it seems *too* egregious so I'll be gladly proven wrong on this, on the other hand... I know I'm bad at code review, but not _that_ bad.
String I/O was completely busted on the Linux guest side as well, I wouldn't be the least bit surprised if KVM were completely broken as well (reviewing now...).
Sean Christopherson
maintainerQuoted 3 lines · Paolo Bonzini
Patches 2 to 7 are a bunch of cleanups to
emulator_pio_inandemulator_pio_in_out, so that the final SEV code is a little easier to reason on. Just a little, no big promises.
IMO, this series goes in the wrong direction and doesn't make the mess any better, just different.
The underlying issue is that kernel_pio() does the completely horrendous thing of consuming vcpu->arch.pio. That leads to the juggling that this series tries to clean up, but it's essentially an impossible problem to solve because the approach itself is broken.
The _only_ reason vcpu->arch.pio (the structure) exists is to snapshot a port I/O operation that didn't originate from the emulator before exiting to userspace, i.e. "fast" I/O and now SEV-ES. Ignoring those two, all info comes from the emulator and a single flag or even the cui pointer would suffice.
Ditto for pio_data, it's purely needed to let userspace read/write values, its use directly in any code except those specific paths is just bad code.
So instead of juggling vcpu->arch.pio.count in weird places, just don't set the damn thing in the first place.
Untested patches attached that frame in where I think we should go with this.
I'll be offline until Monday, apologies for the inconvenience.
From 17384716129668b6636237b410a3885aaf32efb3 Mon Sep 17 00:00:00 2001
From: Sean Christopherson <[email protected]>
Date: Thu, 21 Oct 2021 16:22:27 -0700
Subject: [PATCH 1/6] KVM: x86: Don't exit to userspace when SEV-ES INS is
successful
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/x86.c +1 -1
@@ -12509,7 +12509,7 @@ static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size, vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins; } -return 0 ; +return ret; } int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
From cdb6bceeceda3eb3bd3755b99f00d526e2b9045e Mon Sep 17 00:00:00 2001
From: Sean Christopherson <[email protected]>
Date: Thu, 21 Oct 2021 15:40:36 -0700
Subject: [PATCH 2/6] KVM: x86: WARN if emulated kernel port I/O fails after a
successful iteration
--- arch/x86/kvm/x86.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
arch/x86/kvm/x86.c +10 -4
@@ -7046,7 +7046,7 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,static int kernel_pio (struct kvm_vcpu *vcpu,void *pd) { -int r =0 , i; +int r, i;for (i =0 ; i < vcpu->arch.pio.count; i++) {if (vcpu->arch.pio.in)
@@ -7056,11 +7056,17 @@ static int kernel_pio(struct kvm_vcpu *vcpu, void *pd) r =kvm_io_bus_write (vcpu,KVM_PIO_BUS , vcpu->arch.pio.port, vcpu->arch.pio.size, pd); -if (r) -break ; +if (r) { +/* +* The port doesn't change on subsequent iterations and +* the kernel I/O device should not disappear. +*/ +WARN_ON_ONCE (i); +return r; + } pd += vcpu->arch.pio.size; } -return r; +return 0 ; } static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
From b538f779f15ba63e5e32fd3cce6fae6e530cde40 Mon Sep 17 00:00:00 2001
From: Sean Christopherson <[email protected]>
Date: Thu, 21 Oct 2021 16:45:21 -0700
Subject: [PATCH 3/6] KVM: x86: Use an 'unsigned int' when emulating string
port I/O
--- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +2 -1
@@ -7046,7 +7046,8 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,static int kernel_pio (struct kvm_vcpu *vcpu,void *pd) { -int r, i; +unsigned int i; +int r;for (i =0 ; i < vcpu->arch.pio.count; i++) {if (vcpu->arch.pio.in)
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
From 21f4d5d9048e84d01137ba2a9fbb3d691141dc16 Mon Sep 17 00:00:00 2001
From: Sean Christopherson <[email protected]>
Date: Thu, 21 Oct 2021 15:41:18 -0700
Subject: [PATCH 4/6] KVM: x86: Fill kvm_pio_request if and only if KVM is
exiting to userspace
--- arch/x86/kvm/x86.c | 89 +++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 44 deletions(-)
arch/x86/kvm/x86.c +45 -44
151 lines not on this page · raw message
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
From b134b231b49563ae2fca54dbd4f85356b10aaf53 Mon Sep 17 00:00:00 2001
From: Sean Christopherson <[email protected]>
Date: Thu, 21 Oct 2021 16:29:18 -0700
Subject: [PATCH 5/6] KVM: x86: Stop being clever and use a "completion"
handler for SEV-ES OUTS
--- arch/x86/kvm/x86.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +7 -2
@@ -12481,6 +12481,12 @@ int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int bytes, }EXPORT_SYMBOL_GPL (kvm_sev_es_mmio_read); +static int complete_sev_es_emulated_outs (struct kvm_vcpu *vcpu) +{ + vcpu->arch.pio.count =0 ; +return 1 ; +} +static int complete_sev_es_emulated_ins (struct kvm_vcpu *vcpu) { memcpy(vcpu->arch.guest_ins_data, vcpu->arch.pio_data,
@@ -12500,8 +12506,7 @@ static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, unsigned int size,if (ret)return ret; - vcpu->arch.pio.count =0 ; - + vcpu->arch.complete_userspace_io = complete_sev_es_emulated_outs;return 0 ; }
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
From b0ac37af659b6ce4cb556adc3bda3752db129724 Mon Sep 17 00:00:00 2001
From: Sean Christopherson <[email protected]>
Date: Thu, 21 Oct 2021 16:40:41 -0700
Subject: [PATCH 6/6] KVM: x86: Move pointer for SEV-ES/fast string I/O into
kvm_pio_request
--- arch/x86/include/asm/kvm_host.h | 4 +++- arch/x86/kvm/x86.c | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -1
@@ -385,6 +385,9 @@ struct kvm_pio_request {int in;int port;int size; + +/* Used to handle string I/O that doesn't originate in the emulator. */ +void *string_data; };#define PT64_ROOT_MAX_LEVEL 5
@@ -701,7 +704,6 @@ struct kvm_vcpu_arch {struct kvm_pio_request pio;void *pio_data; -void *guest_ins_data;u8 event_exit_inst_len;
arch/x86/kvm/x86.c +2 -2
@@ -12489,7 +12489,7 @@ static int complete_sev_es_emulated_outs(struct kvm_vcpu *vcpu)static int complete_sev_es_emulated_ins (struct kvm_vcpu *vcpu) { -memcpy (vcpu->arch.guest_ins_data, vcpu->arch.pio_data, +memcpy (vcpu->arch.pio.string_data, vcpu->arch.pio_data, vcpu->arch.pio.count * vcpu->arch.pio.size); vcpu->arch.pio.count =0 ;
@@ -12520,7 +12520,7 @@ static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size,if (ret)return ret; - vcpu->arch.guest_ins_data = data; + vcpu->arch.string_data = data; vcpu->arch.complete_userspace_io = complete_sev_es_emulated_ins;return 0 ; }
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
Paolo Bonzini
maintainerQuoted 11 lines · Sean Christopherson
Quoted 3 lines · Paolo Bonzini
Patches 2 to 7 are a bunch of cleanups to
emulator_pio_inandemulator_pio_in_out, so that the final SEV code is a little easier to reason on. Just a little, no big promises.IMO, this series goes in the wrong direction and doesn't make the mess any better, just different.
The underlying issue is that
kernel_pio()does the completely horrendous thing of consumingvcpu->arch.pio. That leads to the juggling that this series tries to clean up, but it's essentially an impossible problem to solve because the approach itself is broken.
I agree on this, but I disagree that the series does not make the mess any better. At the very least, the new signatures for __emulator_pio_in, complete_emulator_pio_in and emulator_pio_in_out are improvements regarding the _role_ of vcpu->arch.pio*:
- complete_emulator_pio_in clearly takes the values from vcpu->arch.pio, which _is_ the right thing to do for a complete_userspace_io function. This is not clear of emulator_pio_in before the patch
- __emulator_pio_in and emulator_pio_in_out do not take anymore the buffer argument, making it clear that they operate on the internal pio_data buffer and only complete_emulator_pio_in copies out of it. Which yes is horrible, but at least it is clearly visible in the code now.
I managed to clean things up quite satisfactorily with just 6 patches on top of these eight, so I'll post the full series as soon as I finish testing them. 5.15 can then include these to fix the bug at hand.
Paolo