Re: [PATCH 1/2] KVM: x86 emulator: don't update vcpu state if instruction is restarted.

Gleb Natapov
2010-08-01T09:01:47Z

On Sun, Aug 01, 2010 at 11:54:38AM +0300, Avi Kivity wrote:
> On 08/01/2010 11:28 AM, Gleb Natapov wrote:
> >On Sat, Jul 31, 2010 at 08:25:13PM +0300, Avi Kivity wrote:
> >> On 07/29/2010 03:11 PM, Gleb Natapov wrote:
> >>>No need to update vcpu state since instruction is in the middle of the
> >>>emulation.
> >>>
> >>>Signed-off-by: Gleb Natapov<gleb@redhat.com>
> >>>---
> >>> arch/x86/kvm/x86.c | 31 +++++++++++++------------------
> >>> 1 files changed, 13 insertions(+), 18 deletions(-)
> >>>
> >>>diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> >>>index 76fbc32..7e5f075 100644
> >>>--- a/arch/x86/kvm/x86.c
> >>>+++ b/arch/x86/kvm/x86.c
> >>>@@ -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);
> >>>
> >>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.
>
> But non-emulated pio does a skip_emulated_instruction() immediately
> (or so the code in kvm-tpr-opt.c assumes:
>
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.

> >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);
>
> )
>
> --
> error compiling committee.c: too many arguments to function

--
Gleb.