Skip to contentSkip to conversation

x86 emulator: Update EIP even with instructions with no writeback

PATCH In review Mohammed Gamal 2 participants 5 messages Jul 5, 2008 – Jul 6, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86 emulator: Update EIP even with instructions with no writeback

This patch resolves the problem encountered with HLT emulation with FreeDOS's HIMEM XMS Driver.

HLT is the only instruction that goes to the done label unconditionally, causing the EIP value not to be updated which leads to the guest looping forever on the same instruction.

---

 arch/x86/kvm/x86_emulate.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
arch/x86/kvm/x86_emulate.c +3 -1
index dd4efe1..04d7f02 100644
@@ -1769,13 +1769,15 @@ writeback:
 
 	/* Commit shadow register state. */
 	memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
-	kvm_rip_write(ctxt->vcpu, c->eip);
 
 done:
 	if (rc == X86EMUL_UNHANDLEABLE) {
 		c->eip = saved_eip;
 		return -1;
 	}
+	else
+		kvm_rip_write(ctxt->vcpu, c->eip);
+
 	return 0;
 
 twobyte_insn:
#1 lore raw
Quoted 21 lines · Mohammed Gamal

This patch resolves the problem encountered with HLT emulation with FreeDOS's HIMEM XMS Driver.

HLT is the only instruction that goes to the done label unconditionally, causing the EIP value not to be updated which leads to the guest looping forever on the same instruction.

---

 arch/x86/kvm/x86_emulate.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
arch/x86/kvm/x86_emulate.c +0 -1
index dd4efe1..04d7f02 100644
@@ -1769,13 +1769,15 @@ writeback:
 
 	/* Commit shadow register state. */
 	memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
-	kvm_rip_write(ctxt->vcpu, c->eip);

arch/x86/kvm/x86_emulate.c writeback: · line 1781

Quoted 10 lines
arch/x86/kvm/x86_emulate.c +3 -0
 
 done:
 	if (rc == X86EMUL_UNHANDLEABLE) {
 		c->eip = saved_eip;
 		return -1;
 	}
+	else
+		kvm_rip_write(ctxt->vcpu, c->eip);
+
 	return 0;

Why not change hlt to writeback like all other instructions?

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 26 lines · Avi Kivity
Quoted 25 lines · Mohammed Gamal

This patch resolves the problem encountered with HLT emulation with FreeDOS's HIMEM XMS Driver.
HLT is the only instruction that goes to the done label unconditionally, causing the EIP value not to be updated which leads to the guest looping forever on the same instruction.

---

 arch/x86/kvm/x86_emulate.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
arch/x86/kvm/x86_emulate.c +0 -1
index dd4efe1..04d7f02 100644
@@ -1769,13 +1769,15 @@ writeback:
         /* Commit shadow register state. */
       memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
-       kvm_rip_write(ctxt->vcpu, c->eip);
  done:
       if (rc == X86EMUL_UNHANDLEABLE) {
               c->eip = saved_eip;
Quoted 9 lines

arch/x86/kvm/x86_emulate.c writeback: · line 1779

Quoted 6 lines
arch/x86/kvm/x86_emulate.c +3 -0
               return -1;
       }
+       else
+               kvm_rip_write(ctxt->vcpu, c->eip);
+
       return 0;

Why not change hlt to writeback like all other instructions?

IIRC hlt doesn't do writebacks. So, instead of changing hlt to go for a bogus writeback, I thought it'd be more logical that since we're going to the done label anyway we check first if the instruction is unhandleable, in which case we write the saved EIP, otherwise we update the EIP value.

Anyway, here is a patch that changes hlt to writeback.

 arch/x86/kvm/x86_emulate.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kvm/x86_emulate.c +1 -1
index dd4efe1..62e71b6 100644
@@ -1732,7 +1732,7 @@ special_insn:
 		break;
 	case 0xf4:              /* hlt */
 		ctxt->vcpu->arch.halt_request = 1;
-		goto done;
+		break;
 	case 0xf5:	/* cmc */
 		/* complement carry flag from eflags reg */
 		ctxt->eflags ^= EFLG_CF;
#3 lore raw
Quoted 39 lines · Mohammed Gamal
Quoted 26 lines · Avi Kivity
Quoted 24 lines · Mohammed Gamal

This patch resolves the problem encountered with HLT emulation with FreeDOS's HIMEM XMS Driver.
HLT is the only instruction that goes to the done label unconditionally, causing the EIP value not to be updated which leads to the guest looping forever on the same instruction.

---

 arch/x86/kvm/x86_emulate.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
arch/x86/kvm/x86_emulate.c +0 -1
index dd4efe1..04d7f02 100644
@@ -1769,13 +1769,15 @@ writeback:
         /* Commit shadow register state. */
       memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
-       kvm_rip_write(ctxt->vcpu, c->eip);
  done:
       if (rc == X86EMUL_UNHANDLEABLE) {
               c->eip = saved_eip;
Quoted 10 lines

arch/x86/kvm/x86_emulate.c writeback: · line 1780

Quoted 7 lines
arch/x86/kvm/x86_emulate.c +3 -0
               return -1;
       }
+       else
+               kvm_rip_write(ctxt->vcpu, c->eip);
+
       return 0;
 

Why not change hlt to writeback like all other instructions?

Quoted 6 lines

IIRC hlt doesn't do writebacks. So, instead of changing hlt to go for a bogus writeback, I thought it'd be more logical that since we're going to the done label anyway we check first if the instruction is unhandleable, in which case we write the saved EIP, otherwise we update the EIP value.

It's not bogus, you have to write back the instruction pointer at least. It also helps having less code paths.

Quoted 2 lines

Anyway, here is a patch that changes hlt to writeback.

Does it solve the problem? If so, please provide an updated changelog entry and a signoff.

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw
Quoted 51 lines · Avi Kivity
Quoted 43 lines · Mohammed Gamal
Quoted 29 lines · Avi Kivity
Quoted 26 lines · Mohammed Gamal

This patch resolves the problem encountered with HLT emulation with FreeDOS's HIMEM XMS Driver.
HLT is the only instruction that goes to the done label unconditionally, causing the EIP value not to be updated which leads to the guest looping forever on the same instruction.

---

 arch/x86/kvm/x86_emulate.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
arch/x86/kvm/x86_emulate.c +0 -1
index dd4efe1..04d7f02 100644
@@ -1769,13 +1769,15 @@ writeback:
        /* Commit shadow register state. */
      memcpy(ctxt->vcpu->arch.regs, c->regs, sizeof c->regs);
-       kvm_rip_write(ctxt->vcpu, c->eip);
 done:
      if (rc == X86EMUL_UNHANDLEABLE) {
              c->eip = saved_eip;
              return -1;
Quoted 10 lines

arch/x86/kvm/x86_emulate.c writeback: · line 1780

Quoted 6 lines
arch/x86/kvm/x86_emulate.c +3 -0
      }
+       else
+               kvm_rip_write(ctxt->vcpu, c->eip);
+
      return 0;
 

Why not change hlt to writeback like all other instructions?

Quoted 6 lines

IIRC hlt doesn't do writebacks. So, instead of changing hlt to go for a bogus writeback, I thought it'd be more logical that since we're going to the done label anyway we check first if the instruction is unhandleable, in which case we write the saved EIP, otherwise we update the EIP value.

Quoted 3 lines

It's not bogus, you have to write back the instruction pointer at least. It also helps having less code paths.

The instruction pointer would haven been written back anyway as we do go to the done label anyway. But I am convinced with your agrument.

Quoted 5 lines
Quoted 2 lines

Anyway, here is a patch that changes hlt to writeback.

Does it solve the problem? If so, please provide an updated changelog entry and a signoff.

Yes it does, I'll send the updated patch right now.