Skip to contentSkip to conversation

KVM: mmio_fault_cr2 is not used.

PATCH In review Gleb Natapov 2 participants 15 messages Apr 12, 2011 – Apr 26, 2011

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

PATCH 1/5 KVM: mmio_fault_cr2 is not used.

Remove unused variable mmio_fault_cr2.

---
 arch/x86/include/asm/kvm_host.h |    1 -
 arch/x86/kvm/x86.c              |    2 +-
 2 files changed, 1 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -1
index bd57639..c1a7f54 100644
@@ -358,7 +358,6 @@ struct kvm_vcpu_arch {
 	struct fpu guest_fpu;
 	u64 xcr0;
 
-	gva_t mmio_fault_cr2;
 	struct kvm_pio_request pio;
 	void *pio_data;
 
arch/x86/kvm/x86.c +1 -1
index 1d5a7f4..b568779 100644
@@ -4504,7 +4504,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
 	struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;
 
 	kvm_clear_exception_queue(vcpu);
-	vcpu->arch.mmio_fault_cr2 = cr2;
+
 	/*
 	 * TODO: fix emulate.c to use guest_read/write_register
 	 * instead of direct ->regs accesses, can save hundred cycles
Signature · 1 line
1.7.2.3
#1 lore raw

PATCH 2/5 KVM: emulator: sysexit should #GP in non protected mode.

---
 arch/x86/kvm/emulate.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/kvm/emulate.c +2 -0
index 3e8b4ab..eb4c7eb 100644
@@ -1973,6 +1973,8 @@ emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 		cs.d = 0;
 		cs.l = 1;
 		break;
+	default:
+		return emulate_gp(ctxt, 0);
 	}
 	cs_sel |= SELECTOR_RPL_MASK;
 	ss_sel |= SELECTOR_RPL_MASK;
Signature · 1 line
1.7.2.3
#5 lore raw

PATCH 3/5 KVM: emulator: Propagate fault in far jump emulation.

In reply to Gleb Natapov
---
 arch/x86/kvm/emulate.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/kvm/emulate.c +2 -1
index eb4c7eb..4f95249 100644
@@ -3876,7 +3876,8 @@ special_insn:
 	jump_far:
 		memcpy(&sel, c->src.valptr + c->op_bytes, 2);
 
-		if (load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS))
+		rc = load_segment_descriptor(ctxt, ops, sel, VCPU_SREG_CS);
+		if (rc != X86EMUL_CONTINUE)
 			goto done;
 
 		c->eip = 0;
Signature · 1 line
1.7.2.3
#12 lore raw

PATCH 5/5 KVM: call cache_all_regs() only once during instruction emulation.

In reply to Gleb Natapov
---
 arch/x86/kvm/x86.c |   14 ++++++--------
 1 files changed, 6 insertions(+), 8 deletions(-)
arch/x86/kvm/x86.c +6 -8
index 609c7ab..efe196c 100644
@@ -4404,6 +4404,12 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
 	struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;
 	int cs_db, cs_l;
 
+	/*
+	 * TODO: fix emulate.c to use guest_read/write_register
+	 * instead of direct ->regs accesses, can save hundred cycles
+	 * on Intel for instructions that don't read/change RSP, for
+	 * for example.
+	 */
 	cache_all_regs(vcpu);
 
 	kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
@@ -4505,14 +4511,6 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
 
 	kvm_clear_exception_queue(vcpu);
 
-	/*
-	 * TODO: fix emulate.c to use guest_read/write_register
-	 * instead of direct ->regs accesses, can save hundred cycles
-	 * on Intel for instructions that don't read/change RSP, for
-	 * for example.
-	 */
-	cache_all_regs(vcpu);
-
 	if (!(emulation_type & EMULTYPE_NO_DECODE)) {
 		init_emulate_ctxt(vcpu);
 		vcpu->arch.emulate_ctxt.interruptibility = 0;
Signature · 1 line
1.7.2.3
#6 lore raw

PATCH 4/5 KVM: Fix compound mmio.

In reply to Gleb Natapov

mmio_index should be taken into account when copying data from userspace.

---
 arch/x86/kvm/x86.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/kvm/x86.c +2 -1
index b568779..609c7ab 100644
@@ -5518,7 +5518,8 @@ static int complete_mmio(struct kvm_vcpu *vcpu)
 	if (vcpu->mmio_needed) {
 		vcpu->mmio_needed = 0;
 		if (!vcpu->mmio_is_write)
-			memcpy(vcpu->mmio_data, run->mmio.data, 8);
+			memcpy(vcpu->mmio_data + vcpu->mmio_index,
+			       run->mmio.data, 8);
 		vcpu->mmio_index += 8;
 		if (vcpu->mmio_index < vcpu->mmio_size) {
 			run->exit_reason = KVM_EXIT_MMIO;
Signature · 1 line
1.7.2.3
#2 lore raw

Re: [PATCH 2/5] KVM: emulator: sysexit should #GP in non protected mode.

In reply to Gleb Natapov
Quoted 10 lines · Gleb Natapov
---
  arch/x86/kvm/emulate.c |    2 ++
  1 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/kvm/emulate.c +0 -0
index 3e8b4ab..eb4c7eb 100644

arch/x86/kvm/emulate.c emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) · line 1978

Quoted 6 lines
arch/x86/kvm/emulate.c +2 -0
@@ -1973,6 +1973,8 @@ emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
  		cs.d = 0;
  		cs.l = 1;
  		break;
+	default:
+		return emulate_gp(ctxt, 0);
  	}

In addition, looks like the instruction is valid in 16-bit protected mode.

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

Re: [PATCH 4/5] KVM: Fix compound mmio.

In reply to Gleb Natapov
Quoted 13 lines · Gleb Natapov

mmio_index should be taken into account when copying data from userspace.

---
  arch/x86/kvm/x86.c |    3 ++-
  1 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/kvm/x86.c +0 -0
index b568779..609c7ab 100644

arch/x86/kvm/x86.c static int complete_mmio(struct kvm_vcpu *vcpu) · line 5525

Quoted 9 lines
arch/x86/kvm/x86.c +2 -1
@@ -5518,7 +5518,8 @@ static int complete_mmio(struct kvm_vcpu *vcpu)
  	if (vcpu->mmio_needed) {
  		vcpu->mmio_needed = 0;
  		if (!vcpu->mmio_is_write)
-			memcpy(vcpu->mmio_data, run->mmio.data, 8);
+			memcpy(vcpu->mmio_data + vcpu->mmio_index,
+			       run->mmio.data, 8);
  		vcpu->mmio_index += 8;
  		if (vcpu->mmio_index<  vcpu->mmio_size) {
  			run->exit_reason = KVM_EXIT_MMIO;

Interesting, the code passed the emulator.flat sse test. Does it now?

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

Re: [PATCH 4/5] KVM: Fix compound mmio.

Quoted 9 lines · Avi Kivity
Quoted 8 lines · Gleb Natapov

mmio_index should be taken into account when copying data from userspace.

---
 arch/x86/kvm/x86.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
Quoted 17 lines
Quoted 5 lines
arch/x86/kvm/x86.c +0 -0
index b568779..609c7ab 100644

arch/x86/kvm/x86.c static int complete_mmio(struct kvm_vcpu *vcpu) · line 5525

Quoted 9 lines
arch/x86/kvm/x86.c +2 -1
@@ -5518,7 +5518,8 @@ static int complete_mmio(struct kvm_vcpu *vcpu)
 	if (vcpu->mmio_needed) {
 		vcpu->mmio_needed = 0;
 		if (!vcpu->mmio_is_write)
-			memcpy(vcpu->mmio_data, run->mmio.data, 8);
+			memcpy(vcpu->mmio_data + vcpu->mmio_index,
+			       run->mmio.data, 8);
 		vcpu->mmio_index += 8;
 		if (vcpu->mmio_index<  vcpu->mmio_size) {
 			run->exit_reason = KVM_EXIT_MMIO;

Interesting, the code passed the emulator.flat sse test. Does it now?

It pass now and before. Probably by chance. But if I change read_emulated() to do

int n = min(size, (unsigned)KVM_MMIO_SIZE);

instead of

int n = min(size, 8u);

emulator.flat fails to emulate far jump instruction.

--
Gleb.

#3 lore raw

Re: [PATCH 2/5] KVM: emulator: sysexit should #GP in non protected mode.

In reply to Avi Kivity
Quoted 6 lines · Avi Kivity
Quoted 5 lines · Gleb Natapov
---
 arch/x86/kvm/emulate.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
Quoted 14 lines

arch/x86/kvm/emulate.c emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops) · line 1978

Quoted 11 lines
arch/x86/kvm/emulate.c +2 -0
index 3e8b4ab..eb4c7eb 100644
@@ -1973,6 +1973,8 @@ emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
 		cs.d = 0;
 		cs.l = 1;
 		break;
+	default:
+		return emulate_gp(ctxt, 0);
 	}

In addition, looks like the instruction is valid in 16-bit protected mode.

Actually we already check for real mode and vm86 at the start of the function, so the patch is incorrect. Will have to fix compile warning in some other way.

--
Gleb.

#9 lore raw

Re: [PATCH 4/5] KVM: Fix compound mmio.

In reply to Gleb Natapov
Quoted 25 lines · Gleb Natapov
Quoted 24 lines · Avi Kivity
Quoted 22 lines · Gleb Natapov

mmio_index should be taken into account when copying data from userspace.

---
  arch/x86/kvm/x86.c |    3 ++-
  1 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/kvm/x86.c +2 -1
index b568779..609c7ab 100644
@@ -5518,7 +5518,8 @@ static int complete_mmio(struct kvm_vcpu *vcpu)
  	if (vcpu->mmio_needed) {
  		vcpu->mmio_needed = 0;
  		if (!vcpu->mmio_is_write)
-			memcpy(vcpu->mmio_data, run->mmio.data, 8);
+			memcpy(vcpu->mmio_data + vcpu->mmio_index,
+			       run->mmio.data, 8);
  		vcpu->mmio_index += 8;
  		if (vcpu->mmio_index<   vcpu->mmio_size) {
  			run->exit_reason = KVM_EXIT_MMIO;
Quoted 3 lines
Quoted 2 lines

Interesting, the code passed the emulator.flat sse test. Does it now?

It pass now and before. Probably by chance.

I don't understand how. I explicitly set the values so that it would fail in that case.

Can you patch the test to fail with the current code?

Quoted 9 lines

But if I change read_emulated() to do

  int n = min(size, (unsigned)KVM_MMIO_SIZE);

instead of

  int n = min(size, 8u);

emulator.flat fails to emulate far jump instruction.

Ouch, looks like we have the multi-transaction support in two places. I guess this is what made sse mmio work.

Not sure what we should do (patch is fine, question is how to resolve the duplication).

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

Re: [PATCH 2/5] KVM: emulator: sysexit should #GP in non protected mode.

In reply to Gleb Natapov
Quoted 19 lines · Gleb Natapov
Quoted 18 lines · Avi Kivity
Quoted 16 lines · Gleb Natapov
---
  arch/x86/kvm/emulate.c |    2 ++
  1 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/kvm/emulate.c +2 -0
index 3e8b4ab..eb4c7eb 100644
@@ -1973,6 +1973,8 @@ emulate_sysexit(struct x86_emulate_ctxt *ctxt, struct x86_emulate_ops *ops)
  		cs.d = 0;
  		cs.l = 1;
  		break;
+	default:
+		return emulate_gp(ctxt, 0);
  	}
Quoted 6 lines
Quoted 2 lines

In addition, looks like the instruction is valid in 16-bit protected mode.

Actually we already check for real mode and vm86 at the start of the function, so the patch is incorrect. Will have to fix compile warning in some other way.

Right. Note the check for vm86 is unneeded, since we have the Priv tag set and vm86 implies cpl 3.

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

Re: [PATCH 4/5] KVM: Fix compound mmio.

In reply to Avi Kivity
Quoted 26 lines · Avi Kivity
Quoted 25 lines · Gleb Natapov
Quoted 24 lines · Avi Kivity
Quoted 22 lines · Gleb Natapov

mmio_index should be taken into account when copying data from userspace.

---
  arch/x86/kvm/x86.c |    3 ++-
  1 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/kvm/x86.c +2 -1
index b568779..609c7ab 100644
@@ -5518,7 +5518,8 @@ static int complete_mmio(struct kvm_vcpu *vcpu)
  	if (vcpu->mmio_needed) {
  		vcpu->mmio_needed = 0;
  		if (!vcpu->mmio_is_write)
-			memcpy(vcpu->mmio_data, run->mmio.data, 8);
+			memcpy(vcpu->mmio_data + vcpu->mmio_index,
+			       run->mmio.data, 8);
  		vcpu->mmio_index += 8;
  		if (vcpu->mmio_index<   vcpu->mmio_size) {
  			run->exit_reason = KVM_EXIT_MMIO;
Quoted 9 lines
Quoted 3 lines
Quoted 2 lines

Interesting, the code passed the emulator.flat sse test. Does it now?

It pass now and before. Probably by chance.

I don't understand how. I explicitly set the values so that it would fail in that case.

Can you patch the test to fail with the current code?

If I understand correctly you've already found the explanation why test case worked?

Quoted 8 lines
Quoted 8 lines

But if I change read_emulated() to do

int n = min(size, (unsigned)KVM_MMIO_SIZE);

instead of

int n = min(size, 8u);

Quoted 8 lines
Quoted 1 line

emulator.flat fails to emulate far jump instruction.

Ouch, looks like we have the multi-transaction support in two places. I guess this is what made sse mmio work.

Not sure what we should do (patch is fine, question is how to resolve the duplication).

Multi-transaction works faster in complete_mmio, so read_emulated() should use it by doing int n = min(size, (unsigned)KVM_MMIO_SIZE). Other code in read_emulated() is still needed since it provides read re-play for multiple mmio reads during instruction emulation.

--
Gleb.

#11 lore raw

Re: [PATCH 4/5] KVM: Fix compound mmio.

Quoted 31 lines · Gleb Natapov
Quoted 30 lines · Avi Kivity
Quoted 28 lines · Gleb Natapov
Quoted 24 lines · Avi Kivity
  On 04/12/2011 12:36 PM, Gleb Natapov wrote:
  >mmio_index should be taken into account when copying data from
  >userspace.
  >
  >Signed-off-by: Gleb Natapov<[email protected]>
  >---
  >    arch/x86/kvm/x86.c |    3 ++-
  >    1 files changed, 2 insertions(+), 1 deletions(-)
  >
  >diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
  >index b568779..609c7ab 100644
  >--- a/arch/x86/kvm/x86.c
  >+++ b/arch/x86/kvm/x86.c
  >@@ -5518,7 +5518,8 @@ static int complete_mmio(struct kvm_vcpu *vcpu)
  >    	if (vcpu->mmio_needed) {
  >    		vcpu->mmio_needed = 0;
  >    		if (!vcpu->mmio_is_write)
  >-			memcpy(vcpu->mmio_data, run->mmio.data, 8);
  >+			memcpy(vcpu->mmio_data + vcpu->mmio_index,
  >+			       run->mmio.data, 8);
  >    		vcpu->mmio_index += 8;
  >    		if (vcpu->mmio_index<    vcpu->mmio_size) {
  >    			run->exit_reason = KVM_EXIT_MMIO;
Quoted 2 lines

Interesting, the code passed the emulator.flat sse test. Does it now?

It pass now and before. Probably by chance.

Quoted 7 lines
Quoted 5 lines

I don't understand how. I explicitly set the values so that it would fail in that case.

Can you patch the test to fail with the current code?

If I understand correctly you've already found the explanation why test case worked?

Yes, but I used O_APPEND when writing my message.

Quoted 13 lines
Quoted 13 lines
Quoted 9 lines

But if I change read_emulated() to do

  int n = min(size, (unsigned)KVM_MMIO_SIZE);

instead of

  int n = min(size, 8u);

emulator.flat fails to emulate far jump instruction.

Ouch, looks like we have the multi-transaction support in two places. I guess this is what made sse mmio work.

Quoted 7 lines
Quoted 3 lines

Not sure what we should do (patch is fine, question is how to resolve the duplication).

Multi-transaction works faster in complete_mmio, so read_emulated() should use it by doing int n = min(size, (unsigned)KVM_MMIO_SIZE). Other code in read_emulated() is still needed since it provides read re-play for multiple mmio reads during instruction emulation.

Not concerned about speed here.

Core support for large mmio feels correct, but the duplication doesn't.

Signature · 1 line
error compiling committee.c: too many arguments to function
#13 lore raw
In reply to Gleb Natapov

Ping? What about those patches? 2/5 should be dropped, but others are not yet applied too as far as I see.

Quoted 40 lines · Gleb Natapov

Remove unused variable mmio_fault_cr2.

---
 arch/x86/include/asm/kvm_host.h |    1 -
 arch/x86/kvm/x86.c              |    2 +-
 2 files changed, 1 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -1
index bd57639..c1a7f54 100644
@@ -358,7 +358,6 @@ struct kvm_vcpu_arch {
 	struct fpu guest_fpu;
 	u64 xcr0;
 
-	gva_t mmio_fault_cr2;
 	struct kvm_pio_request pio;
 	void *pio_data;
 
arch/x86/kvm/x86.c +1 -1
index 1d5a7f4..b568779 100644
@@ -4504,7 +4504,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
 	struct decode_cache *c = &vcpu->arch.emulate_ctxt.decode;
 
 	kvm_clear_exception_queue(vcpu);
-	vcpu->arch.mmio_fault_cr2 = cr2;
+
 	/*
 	 * TODO: fix emulate.c to use guest_read/write_register
 	 * instead of direct ->regs accesses, can save hundred cycles
Signature · 6 lines
1.7.2.3

--
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.

#14 lore raw
Quoted 2 lines · Gleb Natapov

Ping? What about those patches? 2/5 should be dropped, but others are not yet applied too as far as I see.

Applied now, thanks for the reminder.

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