Skip to contentSkip to conversation

x86 emulator: Fix emulator return values

PATCH In review Mohammed Gamal 3 participants 11 messages Aug 14, 2010 – Aug 15, 2010

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

PATCH 1/2 x86 emulator: Fix emulator return values

Let the emulator return X86EMUL_* return codes instead of hardcoded values.

---
 arch/x86/kvm/emulate.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
arch/x86/kvm/emulate.c +6 -6
index c476a67..c718589 100644
@@ -2434,7 +2434,7 @@ x86_decode_insn(struct x86_emulate_ctxt *ctxt)
 		break;
 #endif
 	default:
-		return -1;
+		return X86EMUL_UNHANDLEABLE;
 	}
 
 	c->op_bytes = def_op_bytes;
@@ -2531,7 +2531,7 @@ done_prefixes:
 	/* Unrecognised? */
 	if (c->d == 0 || (c->d & Undefined)) {
 		DPRINTF("Cannot emulate %02x\n", c->b);
-		return -1;
+		return X86EMUL_UNHANDLEABLE;
 	}
 
 	if (mode == X86EMUL_MODE_PROT64 && (c->d & Stack))
@@ -2720,11 +2720,11 @@ done_prefixes:
 		/* Special instructions do their own operand decoding. */
 	default:
 		c->dst.type = OP_NONE; /* Disable writeback. */
-		return 0;
+		return X86EMUL_CONTINUE;
 	}
 
 done:
-	return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
+	return rc;
 }
 
 int
@@ -3256,7 +3256,7 @@ writeback:
 	ctxt->eip = c->eip;
 
 done:
-	return (rc == X86EMUL_UNHANDLEABLE) ? -1 : 0;
+	return rc;
 
 twobyte_insn:
 	switch (c->b) {
@@ -3558,5 +3558,5 @@ twobyte_insn:
 
 cannot_emulate:
 	DPRINTF("Cannot emulate %02x\n", c->b);
-	return -1;
+	return X86EMUL_UNHANDLEABLE;
 }
Signature · 1 line
1.7.0.4
#1 lore raw

PATCH 2/2 x86: Bail out on unemulated instructions

If emulation fails due to the instruction being unemulated. Return immediately instead of restarting the instruction and infinitely trying to execute it.

---
 arch/x86/kvm/x86.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +6 -0
index 416aa0e..a31db44 100644
@@ -4036,6 +4036,9 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
 		}
 
 		++vcpu->stat.insn_emulation;
+		if (r == X86EMUL_UNHANDLEABLE)
+			return handle_emulation_failure(vcpu);
+
 		if (r)  {
 			if (reexecute_instruction(vcpu, cr2))
 				return EMULATE_DONE;
@@ -4057,6 +4060,9 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
 restart:
 	r = x86_emulate_insn(&vcpu->arch.emulate_ctxt);
 
+	if (r == X86EMUL_UNHANDLEABLE)
+		return handle_emulation_failure(vcpu);
+
 	if (r) { /* emulation failed */
 		if (reexecute_instruction(vcpu, cr2))
 			return EMULATE_DONE;
Signature · 1 line
1.7.0.4
#2 lore raw

Re: [PATCH 2/2] x86: Bail out on unemulated instructions

Quoted 3 lines · Mohammed Gamal

If emulation fails due to the instruction being unemulated. Return immediately instead of restarting the instruction and infinitely trying to execute it.

This is already handled correctly as far as I can see. Sometimes instruction should be retried and reexecute_instruction() checks for that case. If instruction emulation fails in big real mode re-executing instruction will be useless though, so what should be done is to make reexecute_instruction() return false if vcpu is in big real mode and cpu relies on emulation to handle it.

Quoted 36 lines
---
 arch/x86/kvm/x86.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +6 -0
index 416aa0e..a31db44 100644
@@ -4036,6 +4036,9 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
 		}
 
 		++vcpu->stat.insn_emulation;
+		if (r == X86EMUL_UNHANDLEABLE)
+			return handle_emulation_failure(vcpu);
+
 		if (r)  {
 			if (reexecute_instruction(vcpu, cr2))
 				return EMULATE_DONE;
@@ -4057,6 +4060,9 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
 restart:
 	r = x86_emulate_insn(&vcpu->arch.emulate_ctxt);
 
+	if (r == X86EMUL_UNHANDLEABLE)
+		return handle_emulation_failure(vcpu);
+
 	if (r) { /* emulation failed */
 		if (reexecute_instruction(vcpu, cr2))
 			return EMULATE_DONE;
Signature · 6 lines
1.7.0.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
Gleb.

#3 lore raw

Re: [PATCH 2/2] x86: Bail out on unemulated instructions

Quoted 10 lines · Gleb Natapov
Quoted 3 lines · Mohammed Gamal

If emulation fails due to the instruction being unemulated. Return immediately instead of restarting the instruction and infinitely trying to execute it.

This is already handled correctly as far as I can see. Sometimes instruction should be retried and reexecute_instruction() checks for that case. If instruction emulation fails in big real mode re-executing instruction will be useless though, so what should be done is to make reexecute_instruction() return false if vcpu is in big real mode and cpu relies on emulation to handle it.

We don't have a separate mode for big real mode. The emulation modes we have are real and vm86

Quoted 41 lines
Quoted 33 lines

---
 arch/x86/kvm/x86.c |    6 ++++++

 1 files changed, 6 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +3 -0
index 416aa0e..a31db44 100644
@@ -4036,6 +4036,9 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
               }
 
               ++vcpu->stat.insn_emulation;
+             if (r == X86EMUL_UNHANDLEABLE)
+                     return handle_emulation_failure(vcpu);
+
               if (r)  {
                       if (reexecute_instruction(vcpu, cr2))
                               return EMULATE_DONE;
@@ -4057,6 +4060,9 @@ int emulate_instruction(struct kvm_vcpu *vcpu,

 restart:
      r = x86_emulate_insn(&vcpu->arch.emulate_ctxt);

+     if (r == X86EMUL_UNHANDLEABLE)
+             return handle_emulation_failure(vcpu); +
      if (r) { /* emulation failed */
              if (reexecute_instruction(vcpu, cr2)) return EMULATE_DONE; --
1.7.0.4

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
                       Gleb.

#4 lore raw

Re: [PATCH 2/2] x86: Bail out on unemulated instructions

Quoted 5 lines · Mohammed Gamal
Quoted 4 lines · Gleb Natapov
Quoted 3 lines · Mohammed Gamal

If emulation fails due to the instruction being unemulated. Return immediately instead of restarting the instruction and infinitely trying to execute it.

Quoted 9 lines
Quoted 6 lines

This is already handled correctly as far as I can see. Sometimes instruction should be retried and reexecute_instruction() checks for that case. If instruction emulation fails in big real mode re-executing instruction will be useless though, so what should be done is to make reexecute_instruction() return false if vcpu is in big real mode and cpu relies on emulation to handle it.

We don't have a separate mode for big real mode. The emulation modes we have are real and vm86

That doesn't makes the patch right. So we will have to figure something out.

Quoted 33 lines
Quoted 33 lines
Quoted 33 lines

---
 arch/x86/kvm/x86.c |    6 ++++++

 1 files changed, 6 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +3 -0
index 416aa0e..a31db44 100644
@@ -4036,6 +4036,9 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
               }
 
               ++vcpu->stat.insn_emulation;
+             if (r == X86EMUL_UNHANDLEABLE)
+                     return handle_emulation_failure(vcpu);
+
               if (r)  {
                       if (reexecute_instruction(vcpu, cr2))
                               return EMULATE_DONE;
@@ -4057,6 +4060,9 @@ int emulate_instruction(struct kvm_vcpu *vcpu,

 restart:
      r = x86_emulate_insn(&vcpu->arch.emulate_ctxt);

+     if (r == X86EMUL_UNHANDLEABLE)
+             return handle_emulation_failure(vcpu); +
      if (r) { /* emulation failed */
              if (reexecute_instruction(vcpu, cr2)) return EMULATE_DONE; --
1.7.0.4

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
                       Gleb.

--
Gleb.

#5 lore raw

Re: [PATCH 2/2] x86: Bail out on unemulated instructions

2010/8/15 Gleb Natapov <[email protected]>:

Quoted 12 lines · Mohammed Gamal
Quoted 11 lines · Gleb Natapov
Quoted 10 lines · Mohammed Gamal
Quoted 3 lines

If emulation fails due to the instruction being unemulated. Return immediately instead of restarting the instruction and infinitely trying to execute it.

This is already handled correctly as far as I can see. Sometimes instruction should be retried and reexecute_instruction() checks for that case. If instruction emulation fails in big real mode re-executing instruction will be useless though, so what should be done is to make reexecute_instruction() return false if vcpu is in big real mode and cpu relies on emulation to handle it.

Quoted 5 lines
Quoted 3 lines

We don't have a separate mode for big real mode. The emulation modes we have are real and vm86

That doesn't makes the patch right. So we will have to figure something out.

True. Can we do it for real mode in general (i.e. X86EMUL_MODE_REAL)?

Quoted 46 lines
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines

---
 arch/x86/kvm/x86.c |    6 ++++++

 1 files changed, 6 insertions(+), 0 deletions(-)
arch/x86/kvm/x86.c +3 -0
index 416aa0e..a31db44 100644
@@ -4036,6 +4036,9 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
               }
 
               ++vcpu->stat.insn_emulation;
+             if (r == X86EMUL_UNHANDLEABLE)
+                     return handle_emulation_failure(vcpu);
+
               if (r)  {
                       if (reexecute_instruction(vcpu, cr2))
                               return EMULATE_DONE;
@@ -4057,6 +4060,9 @@ int emulate_instruction(struct kvm_vcpu *vcpu,

 restart:
      r = x86_emulate_insn(&vcpu->arch.emulate_ctxt);

+     if (r == X86EMUL_UNHANDLEABLE)
+             return handle_emulation_failure(vcpu); +
      if (r) { /* emulation failed */
              if (reexecute_instruction(vcpu, cr2)) return EMULATE_DONE; --
1.7.0.4

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
                       Gleb.

--
                       Gleb.

#6 lore raw

Re: [PATCH 2/2] x86: Bail out on unemulated instructions

Quoted 13 lines · Mohammed Gamal

2010/8/15 Gleb Natapov<[email protected]>:

Quoted 12 lines · Mohammed Gamal
Quoted 11 lines · Gleb Natapov
Quoted 10 lines · Mohammed Gamal
Quoted 3 lines

If emulation fails due to the instruction being unemulated. Return immediately instead of restarting the instruction and infinitely trying to execute it.

This is already handled correctly as far as I can see. Sometimes instruction should be retried and reexecute_instruction() checks for that case. If instruction emulation fails in big real mode re-executing instruction will be useless though, so what should be done is to make reexecute_instruction() return false if vcpu is in big real mode and cpu relies on emulation to handle it.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

We don't have a separate mode for big real mode. The emulation modes we have are real and vm86

That doesn't makes the patch right. So we will have to figure something out.

True. Can we do it for real mode in general (i.e. X86EMUL_MODE_REAL)?

We can do it conditionally for CPL=0. That includes real mode (and excludes vm86).

However, there's a race involved (see a895e576cfd96). I don't see how we can call handle_emulation_failure() without opening the race again.

Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw

Re: [PATCH 2/2] x86: Bail out on unemulated instructions

In reply to Mohammed Gamal
Quoted 13 lines · Mohammed Gamal

2010/8/15 Gleb Natapov <[email protected]>:

Quoted 12 lines · Mohammed Gamal
Quoted 11 lines · Gleb Natapov
Quoted 10 lines · Mohammed Gamal
Quoted 3 lines

If emulation fails due to the instruction being unemulated. Return immediately instead of restarting the instruction and infinitely trying to execute it.

This is already handled correctly as far as I can see. Sometimes instruction should be retried and reexecute_instruction() checks for that case. If instruction emulation fails in big real mode re-executing instruction will be useless though, so what should be done is to make reexecute_instruction() return false if vcpu is in big real mode and cpu relies on emulation to handle it.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

We don't have a separate mode for big real mode. The emulation modes we have are real and vm86

That doesn't makes the patch right. So we will have to figure something out.

True. Can we do it for real mode in general (i.e. X86EMUL_MODE_REAL)?

If we flush all shadow pages when moving from paged mode to non paged checking for X86EMUL_MODE_REAL sounds enough to me, but Avi knows better. Or we can add is_big_real_mode() callback to x86_ops and implement it in vmx accordingly.

--
Gleb.

#8 lore raw

Re: [PATCH 2/2] x86: Bail out on unemulated instructions

Quoted 6 lines · Gleb Natapov
Quoted 2 lines

True. Can we do it for real mode in general (i.e. X86EMUL_MODE_REAL)?

If we flush all shadow pages when moving from paged mode to non paged checking for X86EMUL_MODE_REAL sounds enough to me, but Avi knows better. Or we can add is_big_real_mode() callback to x86_ops and implement it in vmx accordingly.

Neither are possible. We can have one cpu in big real mode and others in paged mode, so even in real mode we cannot rule out a spurious page fault due to shadow write protection.

Signature · 1 line
error compiling committee.c: too many arguments to function
#9 lore raw

Re: [PATCH 2/2] x86: Bail out on unemulated instructions

Quoted 12 lines · Avi Kivity
Quoted 6 lines · Gleb Natapov
Quoted 2 lines

True. Can we do it for real mode in general (i.e. X86EMUL_MODE_REAL)?

If we flush all shadow pages when moving from paged mode to non paged checking for X86EMUL_MODE_REAL sounds enough to me, but Avi knows better. Or we can add is_big_real_mode() callback to x86_ops and implement it in vmx accordingly.

Neither are possible. We can have one cpu in big real mode and others in paged mode, so even in real mode we cannot rule out a spurious page fault due to shadow write protection.

Correct, just checking X86EMUL_MODE_REAL is not enough due to smp, but why checking for big real mode will not work? If instruction can't be emulated while vcpu is in big real mode returning to vcpu is not an option, so kvm will fail anyway.

--
Gleb.

#10 lore raw

Re: [PATCH 2/2] x86: Bail out on unemulated instructions

Quoted 9 lines · Gleb Natapov
Quoted 5 lines

Neither are possible. We can have one cpu in big real mode and others in paged mode, so even in real mode we cannot rule out a spurious page fault due to shadow write protection.

Correct, just checking X86EMUL_MODE_REAL is not enough due to smp, but why checking for big real mode will not work? If instruction can't be emulated while vcpu is in big real mode returning to vcpu is not an option, so kvm will fail anyway.

Right. I guess we can have an emulation_reason variable which explains why we are emulating (unvirtualizable state, mmu page fault, mmio page fault, unvirtualizable instruction) and decide accordingly. But it's a lot of work.

Signature · 1 line
error compiling committee.c: too many arguments to function