Skip to contentSkip to conversation

kvm/svm: implement new DecodeAssist features

PATCH In review Andre Przywara 3 participants 14 messages Dec 7, 2010

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

cover kvm/svm: implement new DecodeAssist features

Hi,

upcoming AMD CPUs will have a SVM enhancement called DecodeAssist which will provide more information when intercepting certain events. These information allows to skip the instruction fetching and decoding and handle the intercept immediately.
This patch set implements all the features which are documented in the recent AMD manual (APM vol. 2). For details see the patches.

Please review and apply.

Regards,
Andre.

#1 lore raw

PATCH 1/5 kvm/svm: add new SVM feature bit names

the recent APM Vol.2 and the recent AMD CPUID specification describe new CPUID features bits for SVM. Name them here for later usage.

---
 arch/x86/kvm/svm.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
arch/x86/kvm/svm.c +4 -0
index ae943bb..c573e2d 100644
@@ -51,6 +51,10 @@ MODULE_LICENSE("GPL");
 #define SVM_FEATURE_LBRV           (1 <<  1)
 #define SVM_FEATURE_SVML           (1 <<  2)
 #define SVM_FEATURE_NRIP           (1 <<  3)
+#define SVM_FEATURE_TSC_RATE       (1 <<  4)
+#define SVM_FEATURE_VMCB_CLEAN     (1 <<  5)
+#define SVM_FEATURE_FLUSH_ASID     (1 <<  6)
+#define SVM_FEATURE_DECODE_ASSIST  (1 <<  7)
 #define SVM_FEATURE_PAUSE_FILTER   (1 << 10)
 
 #define NESTED_EXIT_HOST	0	/* Exit handled on host level */
Signature · 1 line
1.6.4
#2 lore raw

PATCH 2/5 kvm/svm: enhance MOV CR intercept handler

In reply to Andre Przywara

Newer SVM implementations provide the GPR number in the VMCB, so that the emulation path is no longer necesarry to handle CR register access intercepts. Implement the handling in svm.c and use it when the info is provided.

---
 arch/x86/include/asm/svm.h |    2 +
 arch/x86/kvm/svm.c         |   74 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 68 insertions(+), 8 deletions(-)
arch/x86/include/asm/svm.h +2 -0
index 11dbca7..589fc25 100644
@@ -256,6 +256,8 @@ struct __attribute__ ((__packed__)) vmcb {
 #define SVM_EXITINFOSHIFT_TS_REASON_JMP 38
 #define SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE 44
 
+#define SVM_EXITINFO_REG_MASK 0x0F
+
 #define	SVM_EXIT_READ_CR0 	0x000
 #define	SVM_EXIT_READ_CR3 	0x003
 #define	SVM_EXIT_READ_CR4 	0x004
arch/x86/kvm/svm.c +66 -8

99 lines not on this page · raw message

Signature · 1 line
1.6.4
#3 lore raw

Re: [PATCH 2/5] kvm/svm: enhance MOV CR intercept handler

Quoted 63 lines · Andre Przywara

Newer SVM implementations provide the GPR number in the VMCB, so that the emulation path is no longer necesarry to handle CR register access intercepts. Implement the handling in svm.c and use it when the info is provided.

---
  arch/x86/include/asm/svm.h |    2 +
  arch/x86/kvm/svm.c         |   74 +++++++++++++++++++++++++++++++++++++++-----
  2 files changed, 68 insertions(+), 8 deletions(-)
arch/x86/include/asm/svm.h +2 -0
index 11dbca7..589fc25 100644
@@ -256,6 +256,8 @@ struct __attribute__ ((__packed__)) vmcb {
  #define SVM_EXITINFOSHIFT_TS_REASON_JMP 38
  #define SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE 44
 
+#define SVM_EXITINFO_REG_MASK 0x0F
+
  #define	SVM_EXIT_READ_CR0 	0x000
  #define	SVM_EXIT_READ_CR3 	0x003
  #define	SVM_EXIT_READ_CR4 	0x004
arch/x86/kvm/svm.c +30 -0
index c573e2d..b7233fd 100644
@@ -2594,12 +2594,70 @@ static int emulate_on_interception(struct vcpu_svm *svm)
  	return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
  }
 
+static int cr_interception(struct vcpu_svm *svm)
+{
+	int reg, cr;
+	unsigned long val;
+
+	if (!boot_cpu_has(SVM_FEATURE_DECODE_ASSIST) ||
+	    (svm->vmcb->control.exit_info_1&  (1ULL<<  63)) == 0)
+		return emulate_on_interception(svm);
+
+	reg = svm->vmcb->control.exit_info_1&  SVM_EXITINFO_REG_MASK;
+	cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
+	if (cr>  15) { /* mov to cr */
+		val = kvm_register_read(&svm->vcpu, reg);
+		switch (cr&  0x0F) {
+		case 0:
+			kvm_set_cr0(&svm->vcpu, val);
+			break;
+		case 3:
+			kvm_set_cr3(&svm->vcpu, val);
+			break;
+		case 4:
+			kvm_set_cr4(&svm->vcpu, val);
+			break;
+		case 8:
+			kvm_set_cr8(&svm->vcpu, val&  0xfUL);
+			break;
+		default:
+			WARN(1, "unhandled write to CR%d", cr&  0x0F);
+			return EMULATE_FAIL;
+		}
 

...

arch/x86/kvm/svm.c static int emulate_on_interception(struct vcpu_svm *svm) · line 2654

Quoted 6 lines
arch/x86/kvm/svm.c +6 -0
+	}
+	skip_emulated_instruction(&svm->vcpu);
+
+	return 1;
+}
+

Must not skip_emulated_instruction() if we inject an exception; see how vmx does this (handle_cr).

Quoted 9 lines
arch/x86/kvm/svm.c +1 -4
@@ -2864,14 +2922,14 @@ static int pause_interception(struct vcpu_svm *svm)
  }
 
  static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
-	[SVM_EXIT_READ_CR0]			= emulate_on_interception,
-	[SVM_EXIT_READ_CR3]			= emulate_on_interception,
-	[SVM_EXIT_READ_CR4]			= emulate_on_interception,
-	[SVM_EXIT_READ_CR8]			= emulate_on_interception,
+	[SVM_EXIT_READ_CR0]			= cr_interception,

arch/x86/kvm/svm.c static int pause_interception(struct vcpu_svm *svm) · line 2933

Quoted 10 lines
arch/x86/kvm/svm.c +5 -2
+	[SVM_EXIT_READ_CR3]			= cr_interception,
+	[SVM_EXIT_READ_CR4]			= cr_interception,
+	[SVM_EXIT_READ_CR8]			= cr_interception,
  	[SVM_EXIT_CR0_SEL_WRITE]		= emulate_on_interception,
  	[SVM_EXIT_WRITE_CR0]			= cr0_write_interception,
-	[SVM_EXIT_WRITE_CR3]			= emulate_on_interception,
-	[SVM_EXIT_WRITE_CR4]			= emulate_on_interception,
+	[SVM_EXIT_WRITE_CR3]			= cr_interception,
+	[SVM_EXIT_WRITE_CR4]			= cr_interception,
  	[SVM_EXIT_WRITE_CR8]			= cr8_write_interception,

We could move cr[08]_write_interception into cr_interception, but that takes a bit more thought. Best done later.

Quoted 2 lines
arch/x86/kvm/svm.c +0 -0
  	[SVM_EXIT_READ_DR0]			= emulate_on_interception,
  	[SVM_EXIT_READ_DR1]			= emulate_on_interception,
Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw

Re: [PATCH 2/5] kvm/svm: enhance MOV CR intercept handler

Quoted 30 lines · Avi Kivity
Quoted 29 lines · Andre Przywara

Newer SVM implementations provide the GPR number in the VMCB, so that the emulation path is no longer necesarry to handle CR register access intercepts. Implement the handling in svm.c and use it when the info is provided.

---
  arch/x86/include/asm/svm.h |    2 +
  arch/x86/kvm/svm.c         |   74 +++++++++++++++++++++++++++++++++++++++-----
  2 files changed, 68 insertions(+), 8 deletions(-)
arch/x86/include/asm/svm.h +4 -4
index 11dbca7..589fc25 100644
...
@@ -2864,14 +2922,14 @@ static int pause_interception(struct vcpu_svm *svm)
  }
 
  static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
-	[SVM_EXIT_READ_CR0]			= emulate_on_interception,
-	[SVM_EXIT_READ_CR3]			= emulate_on_interception,
-	[SVM_EXIT_READ_CR4]			= emulate_on_interception,
-	[SVM_EXIT_READ_CR8]			= emulate_on_interception,
+	[SVM_EXIT_READ_CR0]			= cr_interception,
+	[SVM_EXIT_READ_CR3]			= cr_interception,
+	[SVM_EXIT_READ_CR4]			= cr_interception,
+	[SVM_EXIT_READ_CR8]			= cr_interception,
  	[SVM_EXIT_CR0_SEL_WRITE]		= emulate_on_interception,
Quoted 8 lines

arch/x86/include/asm/svm.h static int pause_interception(struct vcpu_svm *svm) · line 2933

Quoted 6 lines
arch/x86/include/asm/svm.h +2 -2
  	[SVM_EXIT_WRITE_CR0]			= cr0_write_interception,
-	[SVM_EXIT_WRITE_CR3]			= emulate_on_interception,
-	[SVM_EXIT_WRITE_CR4]			= emulate_on_interception,
+	[SVM_EXIT_WRITE_CR3]			= cr_interception,
+	[SVM_EXIT_WRITE_CR4]			= cr_interception,
  	[SVM_EXIT_WRITE_CR8]			= cr8_write_interception,

We could move cr[08]_write_interception into cr_interception, but that takes a bit more thought. Best done later.

Yes, I thought about that, too. But since we still have to deal with the emulation code path, it would not make the code easier. But if we overwrite the svm_exit_handlers[] on detecting the SVM feature, this could make more sense. I will check this out.

Thanks for the review, I will address your comments.

Regards,
Andre.

#5 lore raw

Re: [PATCH 2/5] kvm/svm: enhance MOV CR intercept handler

Quoted 41 lines · Andre Przywara
Quoted 32 lines · Avi Kivity
Quoted 31 lines · Andre Przywara

Newer SVM implementations provide the GPR number in the VMCB, so that the emulation path is no longer necesarry to handle CR register access intercepts. Implement the handling in svm.c and use it when the info is provided.

---
  arch/x86/include/asm/svm.h |    2 +
  arch/x86/kvm/svm.c         |   74 
+++++++++++++++++++++++++++++++++++++++-----
  2 files changed, 68 insertions(+), 8 deletions(-)
arch/x86/include/asm/svm.h +4 -4
index 11dbca7..589fc25 100644
...
@@ -2864,14 +2922,14 @@ static int pause_interception(struct 
 vcpu_svm *svm)
  }
 
  static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
-    [SVM_EXIT_READ_CR0]            = emulate_on_interception,
-    [SVM_EXIT_READ_CR3]            = emulate_on_interception,
-    [SVM_EXIT_READ_CR4]            = emulate_on_interception,
-    [SVM_EXIT_READ_CR8]            = emulate_on_interception,
+    [SVM_EXIT_READ_CR0]            = cr_interception,
+    [SVM_EXIT_READ_CR3]            = cr_interception,
+    [SVM_EXIT_READ_CR4]            = cr_interception,
+    [SVM_EXIT_READ_CR8]            = cr_interception,
      [SVM_EXIT_CR0_SEL_WRITE]        = emulate_on_interception,
Quoted 8 lines

arch/x86/include/asm/svm.h static int pause_interception(struct · line 2934

Quoted 6 lines
arch/x86/include/asm/svm.h +2 -2
      [SVM_EXIT_WRITE_CR0]            = cr0_write_interception,
-    [SVM_EXIT_WRITE_CR3]            = emulate_on_interception,
-    [SVM_EXIT_WRITE_CR4]            = emulate_on_interception,
+    [SVM_EXIT_WRITE_CR3]            = cr_interception,
+    [SVM_EXIT_WRITE_CR4]            = cr_interception,
      [SVM_EXIT_WRITE_CR8]            = cr8_write_interception,

We could move cr[08]_write_interception into cr_interception, but that takes a bit more thought. Best done later.

Quoted 2 lines

Yes, I thought about that, too. But since we still have to deal with the emulation code path, it would not make the code easier. But if we overwrite the svm_exit_handlers[] on detecting the SVM feature, this could make more sense. I will check this out.

In fact the correct thing is to make sure kvm_set_cr0() and kvm_set_cr8() do the right thing, so that the emulation path invoked directly (not through a cr intercept) works.

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

PATCH 3/5 kvm/svm: enhance mov DR intercept handler

In reply to Andre Przywara

Newer SVM implementations provide the GPR number in the VMCB, so that the emulation path is no longer necesarry to handle debug register access intercepts. Implement the handling in svm.c and use it when the info is provided.

---
 arch/x86/kvm/svm.c |   55 ++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 39 insertions(+), 16 deletions(-)
arch/x86/kvm/svm.c +39 -16
index b7233fd..369bd85 100644
@@ -2669,6 +2669,29 @@ static int cr0_write_interception(struct vcpu_svm *svm)
 	return r == EMULATE_DONE;
 }
 
+static int dr_interception(struct vcpu_svm *svm)
+{
+	int reg, dr;
+	unsigned long val;
+
+	if (!boot_cpu_has(SVM_FEATURE_DECODE_ASSIST))
+		return emulate_on_interception(svm);
+
+	reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
+	dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
+
+	if (dr > 15) { /* mov to DRn */
+		val = kvm_register_read(&svm->vcpu, reg);
+		kvm_set_dr(&svm->vcpu, dr - 16, val);
+	} else {
+		kvm_get_dr(&svm->vcpu, dr, &val);
+		kvm_register_write(&svm->vcpu, reg, val);
+	}
+
+	skip_emulated_instruction(&svm->vcpu);
+	return 1;
+}
+
 static int cr8_write_interception(struct vcpu_svm *svm)
 {
 	struct kvm_run *kvm_run = svm->vcpu.run;
@@ -2931,22 +2954,22 @@ static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
 	[SVM_EXIT_WRITE_CR3]			= cr_interception,
 	[SVM_EXIT_WRITE_CR4]			= cr_interception,
 	[SVM_EXIT_WRITE_CR8]			= cr8_write_interception,
-	[SVM_EXIT_READ_DR0]			= emulate_on_interception,
-	[SVM_EXIT_READ_DR1]			= emulate_on_interception,
-	[SVM_EXIT_READ_DR2]			= emulate_on_interception,
-	[SVM_EXIT_READ_DR3]			= emulate_on_interception,
-	[SVM_EXIT_READ_DR4]			= emulate_on_interception,
-	[SVM_EXIT_READ_DR5]			= emulate_on_interception,
-	[SVM_EXIT_READ_DR6]			= emulate_on_interception,
-	[SVM_EXIT_READ_DR7]			= emulate_on_interception,
-	[SVM_EXIT_WRITE_DR0]			= emulate_on_interception,
-	[SVM_EXIT_WRITE_DR1]			= emulate_on_interception,
-	[SVM_EXIT_WRITE_DR2]			= emulate_on_interception,
-	[SVM_EXIT_WRITE_DR3]			= emulate_on_interception,
-	[SVM_EXIT_WRITE_DR4]			= emulate_on_interception,
-	[SVM_EXIT_WRITE_DR5]			= emulate_on_interception,
-	[SVM_EXIT_WRITE_DR6]			= emulate_on_interception,
-	[SVM_EXIT_WRITE_DR7]			= emulate_on_interception,
+	[SVM_EXIT_READ_DR0]			= dr_interception,
+	[SVM_EXIT_READ_DR1]			= dr_interception,
+	[SVM_EXIT_READ_DR2]			= dr_interception,
+	[SVM_EXIT_READ_DR3]			= dr_interception,
+	[SVM_EXIT_READ_DR4]			= dr_interception,
+	[SVM_EXIT_READ_DR5]			= dr_interception,
+	[SVM_EXIT_READ_DR6]			= dr_interception,
+	[SVM_EXIT_READ_DR7]			= dr_interception,
+	[SVM_EXIT_WRITE_DR0]			= dr_interception,
+	[SVM_EXIT_WRITE_DR1]			= dr_interception,
+	[SVM_EXIT_WRITE_DR2]			= dr_interception,
+	[SVM_EXIT_WRITE_DR3]			= dr_interception,
+	[SVM_EXIT_WRITE_DR4]			= dr_interception,
+	[SVM_EXIT_WRITE_DR5]			= dr_interception,
+	[SVM_EXIT_WRITE_DR6]			= dr_interception,
+	[SVM_EXIT_WRITE_DR7]			= dr_interception,
 	[SVM_EXIT_EXCP_BASE + DB_VECTOR]	= db_interception,
 	[SVM_EXIT_EXCP_BASE + BP_VECTOR]	= bp_interception,
 	[SVM_EXIT_EXCP_BASE + UD_VECTOR]	= ud_interception,
Signature · 1 line
1.6.4
#7 lore raw

Re: [PATCH 3/5] kvm/svm: enhance mov DR intercept handler

Quoted 15 lines · Andre Przywara

Newer SVM implementations provide the GPR number in the VMCB, so that the emulation path is no longer necesarry to handle debug register access intercepts. Implement the handling in svm.c and use it when the info is provided.

---
arch/x86/kvm/svm.c | 55 ++++++++++++++++++++++++++++++++++++---------------

1 files changed, 39 insertions(+), 16 deletions(-)
arch/x86/kvm/svm.c +0 -0
index b7233fd..369bd85 100644

arch/x86/kvm/svm.c static int cr0_write_interception(struct vcpu_svm *svm) · line 2678

Quoted 10 lines
arch/x86/kvm/svm.c +7 -0
@@ -2669,6 +2669,29 @@ static int cr0_write_interception(struct vcpu_svm *svm)
 	return r == EMULATE_DONE;
 }
 
+static int dr_interception(struct vcpu_svm *svm)
+{
+	int reg, dr;
+	unsigned long val;
+
+	if (!boot_cpu_has(SVM_FEATURE_DECODE_ASSIST))
+		return emulate_on_interception(svm);

Wouldn't it be better to just change the entry in svm_exit_handlers if we detect that feature on init? No unnecessary branching then.

Alex

#8 lore raw

Re: [PATCH 3/5] kvm/svm: enhance mov DR intercept handler

Quoted 28 lines · Alexander Graf
Quoted 18 lines · Andre Przywara

Newer SVM implementations provide the GPR number in the VMCB, so that the emulation path is no longer necesarry to handle debug register access intercepts. Implement the handling in svm.c and use it when the info is provided.

---

 arch/x86/kvm/svm.c |   55 ++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 39 insertions(+), 16 deletions(-)

diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
index b7233fd..369bd85 100644
--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -2669,6 +2669,29 @@ static int cr0_write_interception(struct vcpu_svm *svm)
return r == EMULATE_DONE;
}

Quoted 7 lines

+static int dr_interception(struct vcpu_svm *svm) +{
+ int reg, dr;
+ unsigned long val;
+
+ if (!boot_cpu_has(SVM_FEATURE_DECODE_ASSIST))
+ return emulate_on_interception(svm);

Quoted 2 lines

Wouldn't it be better to just change the entry in svm_exit_handlers if we detect that feature on init? No unnecessary branching then.

I'd rather make them const instead. For something performance critical (unlike this) we can use static_cpu_has().

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

Re: [PATCH 3/5] kvm/svm: enhance mov DR intercept handler

In reply to Andre Przywara
Quoted 19 lines · Andre Przywara

Newer SVM implementations provide the GPR number in the VMCB, so that the emulation path is no longer necesarry to handle debug register access intercepts. Implement the handling in svm.c and use it when the info is provided.

arch/x86/kvm/svm.c +12 -0
+static int dr_interception(struct vcpu_svm *svm)
+{
+	int reg, dr;
+	unsigned long val;
+
+	if (!boot_cpu_has(SVM_FEATURE_DECODE_ASSIST))
+		return emulate_on_interception(svm);
+
+	reg = svm->vmcb->control.exit_info_1&  SVM_EXITINFO_REG_MASK;
+	dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
+
+	if (dr>  15) { /* mov to DRn */

arch/x86/kvm/svm.c static int cr0_write_interception(struct vcpu_svm *svm) · line 2693

Quoted 10 lines
arch/x86/kvm/svm.c +10 -0
+		val = kvm_register_read(&svm->vcpu, reg);
+		kvm_set_dr(&svm->vcpu, dr - 16, val);
+	} else {
+		kvm_get_dr(&svm->vcpu, dr,&val);
+		kvm_register_write(&svm->vcpu, reg, val);
+	}
+
+	skip_emulated_instruction(&svm->vcpu);
+	return 1;
+}

Again, need to handle faults.

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

PATCH 4/5 kvm/svm: implement enhanced INVLPG intercept

In reply to Andre Przywara

When the DecodeAssist feature is available, the linear address is provided in the VMCB on INVLPG intercepts. Use it directly to avoid any decoding and emulation.
This is only useful for shadow paging, though.

---
 arch/x86/kvm/svm.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
arch/x86/kvm/svm.c +6 -1
index 369bd85..3cf2cef 100644
@@ -2586,7 +2586,12 @@ static int iret_interception(struct vcpu_svm *svm)
 
 static int invlpg_interception(struct vcpu_svm *svm)
 {
-	return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
+	if (!boot_cpu_has(SVM_FEATURE_DECODE_ASSIST))
+		return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
+
+	kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
+	skip_emulated_instruction(&svm->vcpu);
+	return 1;
 }
 
 static int emulate_on_interception(struct vcpu_svm *svm)
Signature · 1 line
1.6.4
#11 lore raw

Re: [PATCH 4/5] kvm/svm: implement enhanced INVLPG intercept

Quoted 15 lines · Andre Przywara

When the DecodeAssist feature is available, the linear address is provided in the VMCB on INVLPG intercepts. Use it directly to avoid any decoding and emulation.
This is only useful for shadow paging, though.

---
  arch/x86/kvm/svm.c |    7 ++++++-
  1 files changed, 6 insertions(+), 1 deletions(-)
arch/x86/kvm/svm.c +0 -0
index 369bd85..3cf2cef 100644

arch/x86/kvm/svm.c static int iret_interception(struct vcpu_svm *svm) · line 2590

Quoted 6 lines
arch/x86/kvm/svm.c +2 -1
@@ -2586,7 +2586,12 @@ static int iret_interception(struct vcpu_svm *svm)
 
  static int invlpg_interception(struct vcpu_svm *svm)
  {
-	return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
+	if (!boot_cpu_has(SVM_FEATURE_DECODE_ASSIST))
+		return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;

static_cpu_has()

Quoted 7 lines
arch/x86/kvm/svm.c +4 -0
+
+	kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
+	skip_emulated_instruction(&svm->vcpu);
+	return 1;
  }
 
  static int emulate_on_interception(struct vcpu_svm *svm)
Signature · 1 line
error compiling committee.c: too many arguments to function
#12 lore raw

PATCH 5/5 kvm/svm: copy instruction bytes from VMCB

In reply to Andre Przywara

In case of a nested page fault or an intercepted #PF newer SVM implementations provide a copy of the faulting instruction bytes in the VMCB.
Use these bytes to feed the instruction emulator and avoid the costly guest instruction fetch in this case.

---
 arch/x86/include/asm/kvm_host.h |    3 +++
 arch/x86/include/asm/svm.h      |    4 +++-
 arch/x86/kvm/emulate.c          |    1 +
 arch/x86/kvm/svm.c              |   20 ++++++++++++++++++++
 arch/x86/kvm/vmx.c              |    7 +++++++
 5 files changed, 34 insertions(+), 1 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
index cfbcbfa..3e3a67e 100644
@@ -586,6 +586,9 @@ struct kvm_x86_ops {
 	void (*write_tsc_offset)(struct kvm_vcpu *vcpu, u64 offset);
 
 	void (*get_exit_info)(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2);
+
+	int (*prefetch_instruction)(struct kvm_vcpu *vcpu);
+
 	const struct trace_print_flags *exit_reasons_str;
 };
 
arch/x86/include/asm/svm.h +3 -1
index 589fc25..6d64b1d 100644
@@ -81,7 +81,9 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
 	u64 lbr_ctl;
 	u64 reserved_5;
 	u64 next_rip;
-	u8 reserved_6[816];
+	u8 insn_len;
+	u8 insn_bytes[15];
+	u8 reserved_6[800];
 };
 
 
arch/x86/kvm/emulate.c +1 -0
index 6366735..abff8ff 100644
@@ -525,6 +525,7 @@ static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
 	/* x86 instructions are limited to 15 bytes. */
 	if (eip + size - ctxt->eip > 15)
 		return X86EMUL_UNHANDLEABLE;
+	kvm_x86_ops->prefetch_instruction(ctxt->vcpu);
 	while (size--) {
 		rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
 		if (rc != X86EMUL_CONTINUE)
arch/x86/kvm/svm.c +20 -0
index 3cf2cef..ed94e9a 100644
@@ -464,6 +464,24 @@ static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
 	svm_set_interrupt_shadow(vcpu, 0);
 }
 
+static int prefetch_instruction(struct kvm_vcpu *vcpu)
+{
+	struct vcpu_svm *svm = to_svm(vcpu);
+	uint8_t len;
+	struct fetch_cache *fetch;
+
+	len = svm->vmcb->control.insn_len & 0x0F;
+	if (len == 0)
+		return 1;
+
+	fetch = &svm->vcpu.arch.emulate_ctxt.decode.fetch;
+	fetch->start = kvm_rip_read(&svm->vcpu);
+	fetch->end = fetch->start + len;
+	memcpy(fetch->data, svm->vmcb->control.insn_bytes, len);
+
+	return 0;
+}
+
 static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
 				bool has_error_code, u32 error_code,
 				bool reinject)
@@ -3830,6 +3848,8 @@ static struct kvm_x86_ops svm_x86_ops = {
 	.adjust_tsc_offset = svm_adjust_tsc_offset,
 
 	.set_tdp_cr3 = set_tdp_cr3,
+
+	.prefetch_instruction = prefetch_instruction,
 };
 
 static int __init svm_init(void)
arch/x86/kvm/vmx.c +7 -0
index 72cfdb7..4825545 100644
@@ -1009,6 +1009,11 @@ static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
 	vmx_set_interrupt_shadow(vcpu, 0);
 }
 
+static int prefetch_instruction(struct kvm_vcpu *vcpu)
+{
+	return 1;
+}
+
 static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
 				bool has_error_code, u32 error_code,
 				bool reinject)
@@ -4362,6 +4367,8 @@ static struct kvm_x86_ops vmx_x86_ops = {
 	.adjust_tsc_offset = vmx_adjust_tsc_offset,
 
 	.set_tdp_cr3 = vmx_set_cr3,
+
+	.prefetch_instruction = prefetch_instruction,
 };
 
 static int __init vmx_init(void)
Signature · 1 line
1.6.4
#13 lore raw

Re: [PATCH 5/5] kvm/svm: copy instruction bytes from VMCB

Quoted 13 lines · Andre Przywara

In case of a nested page fault or an intercepted #PF newer SVM implementations provide a copy of the faulting instruction bytes in the VMCB.
Use these bytes to feed the instruction emulator and avoid the costly guest instruction fetch in this case.

arch/x86/include/asm/kvm_host.h +0 -0
index cfbcbfa..3e3a67e 100644

arch/x86/include/asm/kvm_host.h struct kvm_x86_ops { · line 593

Quoted 8 lines
arch/x86/include/asm/kvm_host.h +3 -0
@@ -586,6 +586,9 @@ struct kvm_x86_ops {
  	void (*write_tsc_offset)(struct kvm_vcpu *vcpu, u64 offset);
 
  	void (*get_exit_info)(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2);
+
+	int (*prefetch_instruction)(struct kvm_vcpu *vcpu);
+
  	const struct trace_print_flags *exit_reasons_str;
  };

How about adding a byte array/len parameter to x86_decode_insn()? It could be used to prefill the buffer instead of invoking a callback.

arch/x86/kvm/emulate.c static int do_insn_fetch(struct x86_emulate_ctxt *ctxt, · line 528

Quoted 10 lines
arch/x86/kvm/emulate.c +1 -0
index 6366735..abff8ff 100644
@@ -525,6 +525,7 @@ static int do_insn_fetch(struct x86_emulate_ctxt *ctxt,
  	/* x86 instructions are limited to 15 bytes. */
  	if (eip + size - ctxt->eip>  15)
  		return X86EMUL_UNHANDLEABLE;
+	kvm_x86_ops->prefetch_instruction(ctxt->vcpu);

Even with the callback, this belongs in x86_decode_insn(), not on every fetch.

arch/x86/kvm/svm.c static void skip_emulated_instruction(struct kvm_vcpu *vcpu) · line 467

Quoted 12 lines
arch/x86/kvm/emulate.c +0 -0
  	while (size--) {
  		rc = do_fetch_insn_byte(ctxt, ops, eip++, dest++);
  		if (rc != X86EMUL_CONTINUE)
arch/x86/kvm/svm.c +1 -0
index 3cf2cef..ed94e9a 100644
@@ -464,6 +464,24 @@ static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  	svm_set_interrupt_shadow(vcpu, 0);
  }
 
+static int prefetch_instruction(struct kvm_vcpu *vcpu)

svm_prefetch_instruction()

Quoted 6 lines
arch/x86/kvm/svm.c +6 -0
+{
+	struct vcpu_svm *svm = to_svm(vcpu);
+	uint8_t len;
+	struct fetch_cache *fetch;
+
+	len = svm->vmcb->control.insn_len&  0x0F;

arch/x86/kvm/svm.c static void skip_emulated_instruction(struct kvm_vcpu *vcpu) · line 483

Quoted 10 lines
arch/x86/kvm/svm.c +10 -0
+	if (len == 0)
+		return 1;
+
+	fetch =&svm->vcpu.arch.emulate_ctxt.decode.fetch;
+	fetch->start = kvm_rip_read(&svm->vcpu);
+	fetch->end = fetch->start + len;
+	memcpy(fetch->data, svm->vmcb->control.insn_bytes, len);
+
+	return 0;
+}

svm code shouldn't reach in so deep into the emulator privates.

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