Skip to contentSkip to conversation

KVM: nSVM: Check CR0.CD and CR0.NW on VMRUN of nested guests

PATCH Applied Krish Sadhukhan 2 participants 5 messages Apr 9, 2020 – Apr 16, 2020

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

cover KVM: nSVM: Check CR0.CD and CR0.NW on VMRUN of nested guests

Patch# 1: Adds the KVM check.
Patch# 2: Adds the required #defines for the two CR0 bits. Patch# 3: Adds the kvm-unit-test

[PATCH 1/3] KVM: nSVM: Check for CR0.CD and CR0.NW on VMRUN of nested guests [PATCH 2/3] kvm-unit-tests: SVM: Add #defines for CR0.CD and CR0.NW [PATCH 3/3] kvm-unit-tests: nSVM: Test CR0.CD and CR0.NW combination on VMRUN of

 arch/x86/kvm/svm/nested.c | 4 ++++
 1 file changed, 4 insertions(+)

Krish Sadhukhan (1):
nSVM: Check for CR0.CD and CR0.NW on VMRUN of nested guests

 lib/x86/processor.h |  2 ++
 x86/svm_tests.c     | 28 +++++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)

Krish Sadhukhan (2):
SVM: Add #defines for CR0.CD and CR0.NW
nSVM: Test CR0.CD and CR0.NW combination on VMRUN of nested guests

#1 lore raw

PATCH 1/3 KVM: nSVM: Check for CR0.CD and CR0.NW on VMRUN of nested guests

According to section "Canonicalization and Consistency Checks" in APM vol. 2, the following guest state combination is illegal:

"CR0.CD is zero and CR0.NW is set"

---
 arch/x86/kvm/svm/nested.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kvm/svm/nested.c +4 -0
index 90a1ca9..38e4c2b 100644
@@ -207,6 +207,10 @@ static bool nested_vmcb_checks(struct vmcb *vmcb)
 	if ((vmcb->save.efer & EFER_SVME) == 0)
 		return false;
 
+	if (((vmcb->save.cr0 & X86_CR0_CD) == 0) &&
+	    (vmcb->save.cr0 & X86_CR0_NW))
+		return false;
+
 	if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
 		return false;
 
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/3 kvm-unit-tests: SVM: Add #defines for CR0.CD and CR0.NW

In reply to Krish Sadhukhan
---
 lib/x86/processor.h | 2 ++
 1 file changed, 2 insertions(+)
lib/x86/processor.h +2 -0
index 3adddee..804673b 100644
@@ -32,6 +32,8 @@
 #define X86_CR0_TS     0x00000008
 #define X86_CR0_WP     0x00010000
 #define X86_CR0_AM     0x00040000
+#define X86_CR0_NW     0x20000000
+#define X86_CR0_CD     0x40000000
 #define X86_CR0_PG     0x80000000
 #define X86_CR3_PCID_MASK 0x00000fff
 #define X86_CR4_TSD    0x00000004
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 3/3 kvm-unit-tests: nSVM: Test CR0.CD and CR0.NW combination on VMRUN of nested guests

In reply to Krish Sadhukhan

According to section "Canonicalization and Consistency Checks" in APM vol. 2, the following guest state combination is illegal:

"CR0.CD is zero and CR0.NW is set"

---
 x86/svm_tests.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
x86/svm_tests.c +27 -1
index 16b9dfd..8bdefc5 100644
@@ -1352,15 +1352,41 @@ static void basic_guest_main(struct svm_test *test)
 
 static void svm_guest_state_test(void)
 {
+	test_set_guest(basic_guest_main);
+
+	/*
+	 * Un-setting EFER.SVME is illegal
+	 */
 	u64 efer_saved = vmcb->save.efer;
 	u64 efer = efer_saved;
 
-	test_set_guest(basic_guest_main);
 	report (svm_vmrun() == SVM_EXIT_VMMCALL, "EFER.SVME: %lx", efer);
 	efer &= ~EFER_SVME;
 	vmcb->save.efer = efer;
 	report (svm_vmrun() == SVM_EXIT_ERR, "EFER.SVME: %lx", efer);
 	vmcb->save.efer = efer_saved;
+
+	/*
+	 * Un-setting CR0.CD and setting CR0.NW is illegal combination
+	 */
+	u64 cr0_saved = vmcb->save.cr0;
+	u64 cr0 = cr0_saved;
+
+	cr0 |= X86_CR0_CD;
+	cr0 &= ~X86_CR0_NW;
+	vmcb->save.cr0 = cr0;
+	report (svm_vmrun() == SVM_EXIT_VMMCALL, "CR0: %lx", cr0);
+	cr0 |= X86_CR0_NW;
+	vmcb->save.cr0 = cr0;
+	report (svm_vmrun() == SVM_EXIT_VMMCALL, "CR0: %lx", cr0);
+	cr0 &= ~X86_CR0_NW;
+	cr0 &= ~X86_CR0_CD;
+	vmcb->save.cr0 = cr0;
+	report (svm_vmrun() == SVM_EXIT_VMMCALL, "CR0: %lx", cr0);
+	cr0 |= X86_CR0_NW;
+	vmcb->save.cr0 = cr0;
+	report (svm_vmrun() == SVM_EXIT_ERR, "CR0: %lx", cr0);
+	vmcb->save.cr0 = cr0_saved;
 }
 
 struct svm_test svm_tests[] = {
Signature · 1 line
1.8.3.1
maintainer Applied
#4 lore raw
In reply to Krish Sadhukhan
Quoted 14 lines · Krish Sadhukhan

Patch# 1: Adds the KVM check.
Patch# 2: Adds the required #defines for the two CR0 bits. Patch# 3: Adds the kvm-unit-test

[PATCH 1/3] KVM: nSVM: Check for CR0.CD and CR0.NW on VMRUN of nested guests [PATCH 2/3] kvm-unit-tests: SVM: Add #defines for CR0.CD and CR0.NW [PATCH 3/3] kvm-unit-tests: nSVM: Test CR0.CD and CR0.NW combination on VMRUN of

 arch/x86/kvm/svm/nested.c | 4 ++++
 1 file changed, 4 insertions(+)

Krish Sadhukhan (1):
nSVM: Check for CR0.CD and CR0.NW on VMRUN of nested guests

Quoted 8 lines
 lib/x86/processor.h |  2 ++
 x86/svm_tests.c     | 28 +++++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)

Krish Sadhukhan (2):
SVM: Add #defines for CR0.CD and CR0.NW
nSVM: Test CR0.CD and CR0.NW combination on VMRUN of nested guests

Queued, thanks.

Paolo