Speedup ins instruction emulation a little
cover Speedup ins instruction emulation a little
Recent emulator changes fixed many bugs and shortcomings in string io instructions emulation (ability to do io into mmio region, proper DF flag handling) but emulation became much slower. Those patches bring some performance back.
Gleb Natapov (2):
KVM: x86 emulator: don't update vcpu state if instruction is
restarted.
KVM: x86 emulator: check io permissions only once for string pio
arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 6 ++++++ arch/x86/kvm/x86.c | 32 ++++++++++++++------------------ 3 files changed, 21 insertions(+), 18 deletions(-)
PATCH 1/2 KVM: x86 emulator: don't update vcpu state if instruction is restarted.
No need to update vcpu state since instruction is in the middle of the emulation.
--- arch/x86/kvm/x86.c | 31 +++++++++++++------------------ 1 files changed, 13 insertions(+), 18 deletions(-)
arch/x86/kvm/x86.c +13 -18
@@ -4057,32 +4057,27 @@ restart:return handle_emulation_failure (vcpu); } -toggle_interruptibility (vcpu, vcpu->arch.emulate_ctxt.interruptibility); - kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags); -memcpy (vcpu->arch.regs, c->regs,sizeof c->regs); -kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip); + r =EMULATE_DONE ; -if (vcpu->arch.emulate_ctxt.exception >=0 ) { +if (vcpu->arch.emulate_ctxt.exception >=0 )inject_emulated_exception (vcpu); -return EMULATE_DONE ; - } - -if (vcpu->arch.pio.count) { +else if (vcpu->arch.pio.count) {if (!vcpu->arch.pio.in) vcpu->arch.pio.count =0 ; -return EMULATE_DO_MMIO ; - } - -if (vcpu->mmio_needed) { + r =EMULATE_DO_MMIO ; + }else if (vcpu->mmio_needed) {if (vcpu->mmio_is_write) vcpu->mmio_needed =0 ; -return EMULATE_DO_MMIO ; - } - -if (vcpu->arch.emulate_ctxt.restart) + r =EMULATE_DO_MMIO ; + }else if (vcpu->arch.emulate_ctxt.restart) goto restart; -return EMULATE_DONE ; +toggle_interruptibility (vcpu, vcpu->arch.emulate_ctxt.interruptibility); + kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags); +memcpy (vcpu->arch.regs, c->regs,sizeof c->regs); +kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip); + +return r; }EXPORT_SYMBOL_GPL (emulate_instruction);
Signature · 1 line
1.7.1
PATCH 2/2 KVM: x86 emulator: check io permissions only once for string pio
In reply to Gleb NatapovDo not recheck io permission on every iteration.
--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 6 ++++++ arch/x86/kvm/x86.c | 1 + 3 files changed, 8 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
@@ -216,6 +216,7 @@ struct x86_emulate_ctxt {int interruptibility;bool restart;/* restart string instruction after writeback */ +bool perm_ok;/* do not check permissions if true */ int exception;/* exception that happens during emulation or -1 */ u32 error_code;/* error code for exception */
arch/x86/kvm/emulate.c +6 -0
@@ -2161,9 +2161,15 @@ static bool emulator_io_permited(struct x86_emulate_ctxt *ctxt,struct x86_emulate_ops *ops, u16 port, u16 len) { + if (ctxt->perm_ok) + return true; + if (emulator_bad_iopl(ctxt, ops)) if (!emulator_io_port_access_allowed(ctxt, ops, port, len)) return false; + + ctxt->perm_ok = true; +return true; }
arch/x86/kvm/x86.c +1 -0
@@ -3997,6 +3997,7 @@ int emulate_instruction(struct kvm_vcpu *vcpu,memcpy (c->regs, vcpu->arch.regs,sizeof c->regs); vcpu->arch.emulate_ctxt.interruptibility =0 ; vcpu->arch.emulate_ctxt.exception =-1 ; + vcpu->arch.emulate_ctxt.perm_ok = false; r =x86_decode_insn (&vcpu->arch.emulate_ctxt, &emulate_ops);trace_kvm_emulate_insn_start (vcpu);
Signature · 1 line
1.7.1
Quoted 5 lines · Gleb Natapov
Recent emulator changes fixed many bugs and shortcomings in string io instructions emulation (ability to do io into mmio region, proper DF flag handling) but emulation became much slower. Those patches bring some performance back.
Looks good.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 2/2] KVM: x86 emulator: check io permissions only once for string pio
In reply to Gleb NatapovQuoted 8 lines · Gleb Natapov
Do not recheck io permission on every iteration.
Signed-off-by Gleb Natapov <[email protected]>--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 6 ++++++ arch/x86/kvm/x86.c | 1 + 3 files changed, 8 insertions(+), 0 deletions(-)
Applied 1, please regenerate patch 2.
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
In reply to Gleb NatapovQuoted 48 lines · Gleb Natapov
No need to update vcpu state since instruction is in the middle of the emulation.
Signed-off-by Gleb Natapov<[email protected]>--- arch/x86/kvm/x86.c | 31 +++++++++++++------------------ 1 files changed, 13 insertions(+), 18 deletions(-)arch/x86/kvm/x86.c +7
-17@@ -4057,32 +4057,27 @@ restart:return handle_emulation_failure (vcpu); } -toggle_interruptibility (vcpu, vcpu->arch.emulate_ctxt.interruptibility); - kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags); -memcpy (vcpu->arch.regs, c->regs,sizeof c->regs); -kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip); + r =EMULATE_DONE ; -if (vcpu->arch.emulate_ctxt.exception>=0 ) { +if (vcpu->arch.emulate_ctxt.exception>=0 )inject_emulated_exception (vcpu); -return EMULATE_DONE ; - } - -if (vcpu->arch.pio.count) { +else if (vcpu->arch.pio.count) {if (!vcpu->arch.pio.in) vcpu->arch.pio.count =0 ; -return EMULATE_DO_MMIO ; - } - -if (vcpu->mmio_needed) { + r =EMULATE_DO_MMIO ; + }else if (vcpu->mmio_needed) {if (vcpu->mmio_is_write) vcpu->mmio_needed =0 ; -return EMULATE_DO_MMIO ; - } - -if (vcpu->arch.emulate_ctxt.restart) + r =EMULATE_DO_MMIO ; + }else if (vcpu->arch.emulate_ctxt.restart) goto restart;
arch/x86/kvm/x86.c restart: · line 4083
Quoted 10 lines
arch/x86/kvm/x86.c +6
-1-return EMULATE_DONE ; +toggle_interruptibility (vcpu, vcpu->arch.emulate_ctxt.interruptibility); + kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags); +memcpy (vcpu->arch.regs, c->regs,sizeof c->regs); +kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip); + +return r; }EXPORT_SYMBOL_GPL (emulate_instruction);
What about kvm-tpr-opt.c? It uses rip after pio.
It's true that it usually doesn't go through the emulator.
Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 49 lines · Avi Kivity
Quoted 48 lines · Gleb Natapov
No need to update vcpu state since instruction is in the middle of the emulation.
Signed-off-by Gleb Natapov<[email protected]>--- arch/x86/kvm/x86.c | 31 +++++++++++++------------------ 1 files changed, 13 insertions(+), 18 deletions(-)arch/x86/kvm/x86.c +7
-17@@ -4057,32 +4057,27 @@ restart:return handle_emulation_failure (vcpu); } -toggle_interruptibility (vcpu, vcpu->arch.emulate_ctxt.interruptibility); - kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags); -memcpy (vcpu->arch.regs, c->regs,sizeof c->regs); -kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip); + r =EMULATE_DONE ; -if (vcpu->arch.emulate_ctxt.exception>=0 ) { +if (vcpu->arch.emulate_ctxt.exception>=0 )inject_emulated_exception (vcpu); -return EMULATE_DONE ; - } - -if (vcpu->arch.pio.count) { +else if (vcpu->arch.pio.count) {if (!vcpu->arch.pio.in) vcpu->arch.pio.count =0 ; -return EMULATE_DO_MMIO ; - } - -if (vcpu->mmio_needed) { + r =EMULATE_DO_MMIO ; + }else if (vcpu->mmio_needed) {if (vcpu->mmio_is_write) vcpu->mmio_needed =0 ; -return EMULATE_DO_MMIO ; - } - -if (vcpu->arch.emulate_ctxt.restart) + r =EMULATE_DO_MMIO ; + }else if (vcpu->arch.emulate_ctxt.restart) goto restart;
Quoted 13 lines
arch/x86/kvm/x86.c restart: · line 4083
Quoted 10 lines
arch/x86/kvm/x86.c +6
-1-return EMULATE_DONE ; +toggle_interruptibility (vcpu, vcpu->arch.emulate_ctxt.interruptibility); + kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags); +memcpy (vcpu->arch.regs, c->regs,sizeof c->regs); +kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip); + +return r; }EXPORT_SYMBOL_GPL (emulate_instruction);What about
kvm-tpr-opt.c? It uses rip after pio.
It uses rip _during_ pio. And pio emulation changes rip only at the end of emulation.
Quoted 5 lines
It's true that it usually doesn't go through the emulator.
Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.
--
Gleb.
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 62 lines · Gleb Natapov
Quoted 49 lines · Avi Kivity
Quoted 48 lines · Gleb Natapov
No need to update vcpu state since instruction is in the middle of the emulation.
Signed-off-by Gleb Natapov<[email protected]>--- arch/x86/kvm/x86.c | 31 +++++++++++++------------------ 1 files changed, 13 insertions(+), 18 deletions(-)arch/x86/kvm/x86.c +7
-17@@ -4057,32 +4057,27 @@ restart:return handle_emulation_failure (vcpu); } -toggle_interruptibility (vcpu, vcpu->arch.emulate_ctxt.interruptibility); - kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags); -memcpy (vcpu->arch.regs, c->regs,sizeof c->regs); -kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip); + r =EMULATE_DONE ; -if (vcpu->arch.emulate_ctxt.exception>=0 ) { +if (vcpu->arch.emulate_ctxt.exception>=0 )inject_emulated_exception (vcpu); -return EMULATE_DONE ; - } - -if (vcpu->arch.pio.count) { +else if (vcpu->arch.pio.count) {if (!vcpu->arch.pio.in) vcpu->arch.pio.count =0 ; -return EMULATE_DO_MMIO ; - } - -if (vcpu->mmio_needed) { + r =EMULATE_DO_MMIO ; + }else if (vcpu->mmio_needed) {if (vcpu->mmio_is_write) vcpu->mmio_needed =0 ; -return EMULATE_DO_MMIO ; - } - -if (vcpu->arch.emulate_ctxt.restart) + r =EMULATE_DO_MMIO ; + }else if (vcpu->arch.emulate_ctxt.restart) goto restart;Quoted 12 lines
arch/x86/kvm/x86.c restart: · line 4083
Quoted 10 lines
arch/x86/kvm/x86.c +6
-1-return EMULATE_DONE ; +toggle_interruptibility (vcpu, vcpu->arch.emulate_ctxt.interruptibility); + kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags); +memcpy (vcpu->arch.regs, c->regs,sizeof c->regs); +kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip); + +return r; }EXPORT_SYMBOL_GPL (emulate_instruction);What about
kvm-tpr-opt.c? It uses rip after pio.
Quoted 2 lines
It uses rip _during_ pio. And pio emulation changes rip only at the end of emulation.
But non-emulated pio does a skip_emulated_instruction() immediately (or so the code in kvm-tpr-opt.c assumes:
Quoted 10 lines
static void
vtpr_ioport_write(void *opaque, uint32_t addr, uint32_t val){ CPUState *env = cpu_single_env; uint32_t rip; cpu_synchronize_state(env); rip = env->eip - 2; write_byte_virt(env, rip, 0x66); write_byte_virt(env, rip + 1, 0x90);
)
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 63 lines · Avi Kivity
Quoted 62 lines · Gleb Natapov
Quoted 61 lines · Avi Kivity
Quoted 48 lines · Gleb Natapov
No need to update vcpu state since instruction is in the middle of the emulation.
Signed-off-by Gleb Natapov<[email protected]>--- arch/x86/kvm/x86.c | 31 +++++++++++++------------------ 1 files changed, 13 insertions(+), 18 deletions(-)arch/x86/kvm/x86.c +7
-17@@ -4057,32 +4057,27 @@ restart:return handle_emulation_failure (vcpu); } -toggle_interruptibility (vcpu, vcpu->arch.emulate_ctxt.interruptibility); - kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags); -memcpy (vcpu->arch.regs, c->regs,sizeof c->regs); -kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip); + r =EMULATE_DONE ; -if (vcpu->arch.emulate_ctxt.exception>=0 ) { +if (vcpu->arch.emulate_ctxt.exception>=0 )inject_emulated_exception (vcpu); -return EMULATE_DONE ; - } - -if (vcpu->arch.pio.count) { +else if (vcpu->arch.pio.count) {if (!vcpu->arch.pio.in) vcpu->arch.pio.count =0 ; -return EMULATE_DO_MMIO ; - } - -if (vcpu->mmio_needed) { + r =EMULATE_DO_MMIO ; + }else if (vcpu->mmio_needed) {if (vcpu->mmio_is_write) vcpu->mmio_needed =0 ; -return EMULATE_DO_MMIO ; - } - -if (vcpu->arch.emulate_ctxt.restart) + r =EMULATE_DO_MMIO ; + }else if (vcpu->arch.emulate_ctxt.restart) goto restart;arch/x86/kvm/x86.c restart: · line 4083
Quoted 10 lines
arch/x86/kvm/x86.c +6
-1-return EMULATE_DONE ; +toggle_interruptibility (vcpu, vcpu->arch.emulate_ctxt.interruptibility); + kvm_x86_ops->set_rflags (vcpu, vcpu->arch.emulate_ctxt.eflags); +memcpy (vcpu->arch.regs, c->regs,sizeof c->regs); +kvm_rip_write (vcpu, vcpu->arch.emulate_ctxt.eip); + +return r; }EXPORT_SYMBOL_GPL (emulate_instruction);What about
kvm-tpr-opt.c? It uses rip after pio.
Quoted 6 lines
Quoted 2 lines
It uses rip _during_ pio. And pio emulation changes rip only at the end of emulation.
But non-emulated pio does a
skip_emulated_instruction()immediately (or so the code inkvm-tpr-opt.cassumes:
Indeed, this is bug in non-emulated pio. But the patch does not change rip behaviour for emulated pio. vcpu->arch.emulate_ctxt.eip is updated only at the end of emulation.
Quoted 5 lines
Quoted 5 lines
static void
vtpr_ioport_write(void *opaque, uint32_t addr, uint32_t val){ CPUState *env = cpu_single_env; uint32_t rip;
Quoted 10 lines
Quoted 5 lines
cpu_synchronize_state(env); rip = env->eip - 2; write_byte_virt(env, rip, 0x66); write_byte_virt(env, rip + 1, 0x90);)
Signature · 1 line
error compiling committee.c: too many arguments to function
--
Gleb.
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 7 lines · Gleb Natapov
Quoted 6 lines
Quoted 3 lines
It uses rip _during_ pio. And pio emulation changes rip only at the end of emulation.
But non-emulated pio does a
skip_emulated_instruction()immediately (or so the code inkvm-tpr-opt.cassumes:Indeed, this is bug in non-emulated pio.
But userspace depends on this bug.
Quoted 3 lines
But the patch does not change
rip behaviour for emulated pio.vcpu->arch.emulate_ctxt.eipis updated only at the end of emulation.
That will lead to failures if the emulator is used for the kvm-tpr-opt pio (which may happen with big real mode).
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 10 lines · Avi Kivity
Quoted 7 lines · Gleb Natapov
Quoted 6 lines
Quoted 3 lines
It uses rip _during_ pio. And pio emulation changes rip only at the end of emulation.
But non-emulated pio does a
skip_emulated_instruction()immediately (or so the code inkvm-tpr-opt.cassumes:Indeed, this is bug in non-emulated pio.
But userspace depends on this bug.
We can fix that, or make it smarter. Look for io instruction at rip/rip-2 and use rip accordingly for instance.
Quoted 8 lines
Quoted 4 lines
But the patch does not change
rip behaviour for emulated pio.vcpu->arch.emulate_ctxt.eipis updated only at the end of emulation.That will lead to failures if the emulator is used for the kvm-tpr-opt pio (which may happen with big real mode).
IIRC it was always this way in emulator. I'd rather fix userspace than break emulator.
--
Gleb.
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 11 lines · Gleb Natapov
Quoted 8 lines · Avi Kivity
Quoted 6 lines · Gleb Natapov
Quoted 5 lines
Quoted 2 lines
It uses rip _during_ pio. And pio emulation changes rip only at the end of emulation.
But non-emulated pio does a
skip_emulated_instruction()immediately (or so the code inkvm-tpr-opt.cassumes:Indeed, this is bug in non-emulated pio.
But userspace depends on this bug.
We can fix that, or make it smarter. Look for io instruction at rip/rip-2 and use rip accordingly for instance.
That requires everyone to update, or suffer major breakage.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
But the patch does not change
rip behaviour for emulated pio.vcpu->arch.emulate_ctxt.eipis updated only at the end of emulation.That will lead to failures if the emulator is used for the kvm-tpr-opt pio (which may happen with big real mode).
IIRC it was always this way in emulator. I'd rather fix userspace than break emulator.
It wasn't a problem because the emulator wasn't (and still isn't) used for this. But it has the potential to break badly once we make emulate_invalid_guest_state=1 the default.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 8 lines · Avi Kivity
Quoted 7 lines · Gleb Natapov
Quoted 6 lines · Avi Kivity
Quoted 5 lines · Gleb Natapov
Quoted 5 lines
Quoted 2 lines
It uses rip _during_ pio. And pio emulation changes rip only at the end of emulation.
But non-emulated pio does a
skip_emulated_instruction()immediately (or so the code inkvm-tpr-opt.cassumes:
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line
Indeed, this is bug in non-emulated pio.
But userspace depends on this bug.
We can fix that, or make it smarter. Look for io instruction at rip/rip-2 and use rip accordingly for instance.
That requires everyone to update, or suffer major breakage.
They will suffer major breakage when they update to a kvm that calls to kvm-tpr-opt.c from emulator anyway.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
But the patch does not change
rip behaviour for emulated pio.vcpu->arch.emulate_ctxt.eipis updated only at the end of emulation.That will lead to failures if the emulator is used for the kvm-tpr-opt pio (which may happen with big real mode).
Quoted 7 lines
Quoted 2 lines
IIRC it was always this way in emulator. I'd rather fix userspace than break emulator.
It wasn't a problem because the emulator wasn't (and still isn't) used for this. But it has the potential to break badly once we make emulate_invalid_guest_state=1 the default.
So what can we do about it?
--
Gleb.
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 5 lines · Gleb Natapov
Quoted 3 lines
That requires everyone to update, or suffer major breakage.
They will suffer major breakage when they update to a kvm that calls to
kvm-tpr-opt.cfrom emulator anyway.
Why?
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
IIRC it was always this way in emulator. I'd rather fix userspace than break emulator.
It wasn't a problem because the emulator wasn't (and still isn't) used for this. But it has the potential to break badly once we make emulate_invalid_guest_state=1 the default.
So what can we do about it?
Keep the existing behaviour.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 9 lines · Avi Kivity
Quoted 5 lines · Gleb Natapov
Quoted 3 lines
That requires everyone to update, or suffer major breakage.
They will suffer major breakage when they update to a kvm that calls to
kvm-tpr-opt.cfrom emulator anyway.Why?
Because tpr code will be called with wrong rip. Emulator always updated rip at the end of an instruction emulation in writeback stage.
Quoted 11 lines
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
IIRC it was always this way in emulator. I'd rather fix userspace than break emulator.
It wasn't a problem because the emulator wasn't (and still isn't) used for this. But it has the potential to break badly once we make emulate_invalid_guest_state=1 the default.
So what can we do about it?
Keep the existing behaviour.
Existing behaviour will cause breakage.
--
Gleb.
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 11 lines · Gleb Natapov
Quoted 7 lines · Avi Kivity
Quoted 4 lines · Gleb Natapov
Quoted 2 lines
That requires everyone to update, or suffer major breakage.
They will suffer major breakage when they update to a kvm that calls to
kvm-tpr-opt.cfrom emulator anyway.Why?
Because tpr code will be called with wrong rip. Emulator always updated rip at the end of an instruction emulation in writeback stage.
We can change it before switching enabling e_i_g_s by default.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
So what can we do about it?
Keep the existing behaviour.
Existing behaviour will cause breakage.
The existing user-visible behaviour. The user doesn't know whether the emulator is involved or not.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 7 lines · Avi Kivity
Quoted 6 lines · Gleb Natapov
Quoted 5 lines · Avi Kivity
Quoted 4 lines · Gleb Natapov
Quoted 2 lines
That requires everyone to update, or suffer major breakage.
They will suffer major breakage when they update to a kvm that calls to
kvm-tpr-opt.cfrom emulator anyway.
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
Why?
Because tpr code will be called with wrong rip. Emulator always updated rip at the end of an instruction emulation in writeback stage.
We can change it before switching enabling
e_i_g_sby default.
Break emulator? We can't increment rip for all instructions before emulation since then exception will be injected at incorrect rip. Adding code that rollbacks rip in case of exception will complicate things and exception is not the only reason to keep rip pointed to the instruction. We may want to reenter guest to reexecute it for instance.
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
So what can we do about it?
Keep the existing behaviour.
Existing behaviour will cause breakage.
The existing user-visible behaviour. The user doesn't know whether the emulator is involved or not.
When we are going to enable e_i_g_s by default? May be we have enough time to fix userspace? Too ancient userspace already does not run on recent kvm. Or may be we can make userspace enable e_i_g_s per guest. This way userspace that knows it is OK can tell kernel so.
--
Gleb.
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 2 lines · Gleb Natapov
When we are going to enable
e_i_g_sby default?
Optimistically, 2.6.37, so six months.
Quoted 2 lines
May be we have enough
time to fix userspace?
Sure we do, but will users update?
0.12 is mature enough that some users will forget about it and not update it.
Quoted 3 lines
Too ancient userspace already does not run on recent
kvm. Or may be we can make userspace enablee_i_g_sper guest. This way userspace that knows it is OK can tell kernel so.
Let's make it the other way round, enable the optimization for userspace that declares that it does not make use of rip during emulation (kvm-tpr-opt can be changed by queueing a signal and re-entering the guest to complete the operation).
Later we can make the optimization unconditional.
Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 6 lines · Avi Kivity
Quoted 2 lines · Gleb Natapov
When we are going to enable
e_i_g_sby default?Optimistically, 2.6.37, so six months.
Quoted 8 lines
Quoted 2 lines
May be we have enough
time to fix userspace?Sure we do, but will users update?
0.12 is mature enough that some users will forget about it and not update it.
So they will not update kernel too. 0.12/2.6.32 should be mature combo. And we can add patch to 0.12/0.13 stable to work on newer kernels. But realistically the problem will occur only if TPR access is done from big real mode by Windows XP running on old Intel cpus. What are the chances that this will be a problem in practice?
Quoted 11 lines
Quoted 3 lines
Too ancient userspace already does not run on recent
kvm. Or may be we can make userspace enablee_i_g_sper guest. This way userspace that knows it is OK can tell kernel so.Let's make it the other way round, enable the optimization for userspace that declares that it does not make use of rip during emulation (kvm-tpr-opt can be changed by queueing a signal and re-entering the guest to complete the operation).
Later we can make the optimization unconditional.
What do you call "optimization"? e_i_g_s=1? Isn't it the same as I proposed then?
--
Gleb.
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 7 lines · Gleb Natapov
Quoted 6 lines · Avi Kivity
Quoted 1 line · Gleb Natapov
When we are going to enable
e_i_g_sby default?Optimistically, 2.6.37, so six months.
Quoted 2 lines
May be we have enough
time to fix userspace?
Quoted 7 lines
Quoted 5 lines
Sure we do, but will users update?
0.12 is mature enough that some users will forget about it and not update it.
So they will not update kernel too. 0.12/2.6.32 should be mature combo. And we can add patch to 0.12/0.13 stable to work on newer kernels.
We don't know what they'll do. API stability means we only change things to fix bugs.
Quoted 4 lines
But
realistically the problem will occur only if TPR access is done from big real mode by Windows XP running on old Intel cpus. What are the chances that this will be a problem in practice?
Windows XP does use big real mode (I think unintentionally, some segment registers aren't cleared).
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Too ancient userspace already does not run on recent
kvm. Or may be we can make userspace enablee_i_g_sper guest. This way userspace that knows it is OK can tell kernel so.Let's make it the other way round, enable the optimization for userspace that declares that it does not make use of rip during emulation (kvm-tpr-opt can be changed by queueing a signal and re-entering the guest to complete the operation).
Quoted 4 lines
Quoted 2 lines
Later we can make the optimization unconditional.
What do you call "optimization"? e_i_g_s=1? Isn't it the same as I proposed then?
The optimization is your patch.
Step 1: only enable the optimization if userspace indicates it can handle it. <time passes>
Step 2: drop the backwards compatibility code, always enable the optimization.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 10 lines · Avi Kivity
Quoted 9 lines · Gleb Natapov
Quoted 8 lines · Avi Kivity
Quoted 1 line · Gleb Natapov
When we are going to enable
e_i_g_sby default?Optimistically, 2.6.37, so six months.
Quoted 2 lines
May be we have enough
time to fix userspace?Sure we do, but will users update?
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
0.12 is mature enough that some users will forget about it and not update it.
So they will not update kernel too. 0.12/2.6.32 should be mature combo. And we can add patch to 0.12/0.13 stable to work on newer kernels.
We don't know what they'll do. API stability means we only change things to fix bugs.
Is this API documented? Do we guaranty somewhere anywhere that rip during io point past the instruction? I think it should be documented that cpu state cannot be accessed during io emulation. And we can preserve old behaviour for old guests by disabling e_i_g_s for them.
Quoted 8 lines
Quoted 5 lines
But
realistically the problem will occur only if TPR access is done from big real mode by Windows XP running on old Intel cpus. What are the chances that this will be a problem in practice?Windows XP does use big real mode (I think unintentionally, some segment registers aren't cleared).
How it works now then? If it works because Windows XP doesn't realize it runs in big real mode so it doesn't actually access past segment limit why starting emulating it? Boot will take much more time without any gain. And finally does it access TPR while running in big real mode?
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines
Too ancient userspace already does not run on recent
kvm. Or may be we can make userspace enablee_i_g_sper guest. This way userspace that knows it is OK can tell kernel so.Let's make it the other way round, enable the optimization for userspace that declares that it does not make use of rip during emulation (kvm-tpr-opt can be changed by queueing a signal and re-entering the guest to complete the operation).
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Later we can make the optimization unconditional.
What do you call "optimization"? e_i_g_s=1? Isn't it the same as I proposed then?
The optimization is your patch.
I think there is misunderstanding here. My patch does not change anything in this regards. If io exit to userspace is done from emulator rip will point to io instruction with or without my patch and it was always this way.
Quoted 8 lines
Step 1: only enable the optimization if userspace indicates it can handle it.
<time passes>
Step 2: drop the backwards compatibility code, always enable the optimization.Signature · 1 line
error compiling committee.c: too many arguments to function
--
Gleb.
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 6 lines · Gleb Natapov
Quoted 3 lines
We don't know what they'll do. API stability means we only change things to fix bugs.
Is this API documented? Do we guaranty somewhere anywhere that rip during io point past the instruction? I think it should be documented that cpu state cannot be accessed during io emulation.
The user code was written before the documentation.
Quoted 2 lines
And we can preserve old
behaviour for old guests by disablinge_i_g_sfor them.
But the user will see a crash first and report a bug. That's not the experience we want users to have.
Quoted 5 lines
Quoted 2 lines
Windows XP does use big real mode (I think unintentionally, some segment registers aren't cleared).
How it works now then? If it works because Windows XP doesn't realize it runs in big real mode so it doesn't actually access past segment limit why starting emulating it?
IIRC it leaves fs and gs pointing to large segments, but it never accesses them. Since we can't tell whether the guest will use those segments, we can't avoid emulating big real mode. Right now most things work, but that's because we hacked around everything.
Quoted 2 lines
Boot will take much more time
without any gain.
The gain is correctness.
Quoted 2 lines
And finally does it access TPR while running in big real mode?
I don't think so.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
What do you call "optimization"? e_i_g_s=1? Isn't it the same as I proposed then?
The optimization is your patch.
I think there is misunderstanding here. My patch does not change anything in this regards. If io exit to userspace is done from emulator rip will point to io instruction with or without my patch and it was always this way.
In that case the whole thing is moot. When we set eigs=1 we'll have to test Windows XP and hack around it if needed.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 10 lines · Avi Kivity
Quoted 6 lines · Gleb Natapov
Quoted 3 lines
We don't know what they'll do. API stability means we only change things to fix bugs.
Is this API documented? Do we guaranty somewhere anywhere that rip during io point past the instruction? I think it should be documented that cpu state cannot be accessed during io emulation.
The user code was written before the documentation.
We did it with unmapped pages in the middles of the slot recently.
Quoted 5 lines
Quoted 2 lines
And we can preserve old
behaviour for old guests by disablinge_i_g_sfor them.But the user will see a crash first and report a bug. That's not the experience we want users to have.
Definitely. That is why I propose enabling e_i_g_s only if qemu acknowledge that it can use it properly.
Quoted 12 lines
Quoted 6 lines
Quoted 3 lines
Windows XP does use big real mode (I think unintentionally, some segment registers aren't cleared).
How it works now then? If it works because Windows XP doesn't realize it runs in big real mode so it doesn't actually access past segment limit why starting emulating it?
IIRC it leaves fs and gs pointing to large segments, but it never accesses them. Since we can't tell whether the guest will use those segments, we can't avoid emulating big real mode. Right now most things work, but that's because we hacked around everything.
We have logic in TPR patching code that tries to detect WindowsXP guest and if XP is detected it enables vapic. We can disable e_i_g_s if vapic is enabled.
Quoted 5 lines
Quoted 2 lines
Boot will take much more time
without any gain.The gain is correctness.
Agree. Worthy goal.
Quoted 10 lines
Quoted 2 lines
And finally does it access TPR while running in big real mode?
I don't think so.
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
What do you call "optimization"? e_i_g_s=1? Isn't it the same as I proposed then?
The optimization is your patch.
Quoted 10 lines
Quoted 4 lines
I think there is misunderstanding here. My patch does not change anything in this regards. If io exit to userspace is done from emulator rip will point to io instruction with or without my patch and it was always this way.
In that case the whole thing is moot. When we set eigs=1 we'll have to test Windows XP and hack around it if needed.
Signature · 1 line
error compiling committee.c: too many arguments to function
--
Gleb.
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 10 lines · Gleb Natapov
Quoted 8 lines · Avi Kivity
Quoted 5 lines · Gleb Natapov
Quoted 2 lines
We don't know what they'll do. API stability means we only change things to fix bugs.
Is this API documented? Do we guaranty somewhere anywhere that rip during io point past the instruction? I think it should be documented that cpu state cannot be accessed during io emulation.
The user code was written before the documentation.
We did it with unmapped pages in the middles of the slot recently.
What guests did we break?
Quoted 8 lines
Quoted 5 lines
IIRC it leaves fs and gs pointing to large segments, but it never accesses them. Since we can't tell whether the guest will use those segments, we can't avoid emulating big real mode. Right now most things work, but that's because we hacked around everything.
We have logic in TPR patching code that tries to detect WindowsXP guest and if XP is detected it enables vapic. We can disable
e_i_g_sif vapic is enabled.
That code is in userspace. If we can change userspace, the whole problem is gone.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 5 lines · Avi Kivity
Quoted 4 lines · Gleb Natapov
Quoted 3 lines · Avi Kivity
Quoted 2 lines · Gleb Natapov
Quoted 2 lines
We don't know what they'll do. API stability means we only change things to fix bugs.
Quoted 9 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
Is this API documented? Do we guaranty somewhere anywhere that rip during io point past the instruction? I think it should be documented that cpu state cannot be accessed during io emulation.
The user code was written before the documentation.
We did it with unmapped pages in the middles of the slot recently.
What guests did we break?
The one that uses device assignment with old qemu-kvm userspace. Old qemu-kvm copied assigned card's ROM into memory and made it read only (mprotect(RO)) to get MMIO exists on it. Even older device assignment code unmapped one page in the middle of a slot to get mmio exist if the page is accessed (and this breaks if mmap decides to mmap something else there).
Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
IIRC it leaves fs and gs pointing to large segments, but it never accesses them. Since we can't tell whether the guest will use those segments, we can't avoid emulating big real mode. Right now most things work, but that's because we hacked around everything.
We have logic in TPR patching code that tries to detect WindowsXP guest and if XP is detected it enables vapic. We can disable
e_i_g_sif vapic is enabled.That code is in userspace. If we can change userspace, the whole problem is gone.
Userspace calls kvm_enable_vapic() to let kernel know that vapic is enabled.
--
Gleb.
Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.
Quoted 10 lines · Gleb Natapov
Quoted 4 lines
Quoted 2 lines
We did it with unmapped pages in the middles of the slot recently.
What guests did we break?
The one that uses device assignment with old qemu-kvm userspace. Old qemu-kvm copied assigned card's ROM into memory and made it read only (mprotect(RO)) to get MMIO exists on it. Even older device assignment code unmapped one page in the middle of a slot to get mmio exist if the page is accessed (and this breaks if mmap decides to mmap something else there).
That's fine, device assignment is specialized enough. I'm more worried about everyday workloads.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
IIRC it leaves fs and gs pointing to large segments, but it never accesses them. Since we can't tell whether the guest will use those segments, we can't avoid emulating big real mode. Right now most things work, but that's because we hacked around everything.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
We have logic in TPR patching code that tries to detect WindowsXP guest and if XP is detected it enables vapic. We can disable
e_i_g_sif vapic is enabled.That code is in userspace. If we can change userspace, the whole problem is gone.
Userspace calls
kvm_enable_vapic()to let kernel know that vapic is enabled.
This happens before the vapic is detected.
In fact, this particular port is during option rom initialization; I don't think it's in big real mode.
Signature · 1 line
error compiling committee.c: too many arguments to function