Skip to contentSkip to conversation

nSVM: Added test for VGIF feature

PATCH In review Lara Lazier 2 participants 2 messages Jul 22, 2021 – Aug 2, 2021 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nSVM: Added test for VGIF feature

When VGIF is enabled STGI executed in guest mode
sets bit 9, while CLGI clears bit 9 in the int_ctl (offset 60h) of the VMCB.

---
 lib/x86/processor.h |  1 +
 x86/svm.c           |  5 ++++
 x86/svm.h           |  7 +++++
 x86/svm_tests.c     | 70 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 83 insertions(+)
lib/x86/processor.h +1 -0
index 173520f..f4d1757 100644
@@ -187,6 +187,7 @@ static inline bool is_intel(void)
 #define	X86_FEATURE_AMD_IBPB		(CPUID(0x80000008, 0, EBX, 12))
 #define	X86_FEATURE_NPT			(CPUID(0x8000000A, 0, EDX, 0))
 #define	X86_FEATURE_NRIPS		(CPUID(0x8000000A, 0, EDX, 3))
+#define	X86_FEATURE_VGIF		(CPUID(0x8000000A, 0, EDX, 16))
 
 
 static inline bool this_cpu_has(u64 feature)
x86/svm.c +5 -0
index 4eaa97e..b0096a9 100644
@@ -65,6 +65,11 @@ bool default_supported(void)
     return true;
 }
 
+bool vgif_supported(void)
+{
+	return this_cpu_has(X86_FEATURE_VGIF);
+}
+
 void default_prepare(struct svm_test *test)
 {
 	vmcb_ident(vmcb);
x86/svm.h +7 -0
index 995b0f8..339b9f7 100644
@@ -115,6 +115,12 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
 #define V_IRQ_SHIFT 8
 #define V_IRQ_MASK (1 << V_IRQ_SHIFT)
 
+#define V_GIF_ENABLED_SHIFT 25
+#define V_GIF_ENABLED_MASK (1 << V_GIF_ENABLED_SHIFT)
+
+#define V_GIF_SHIFT 9
+#define V_GIF_MASK (1 << V_GIF_SHIFT)
+
 #define V_INTR_PRIO_SHIFT 16
 #define V_INTR_PRIO_MASK (0x0f << V_INTR_PRIO_SHIFT)
 
@@ -398,6 +404,7 @@ u64 *npt_get_pdpe(void);
 u64 *npt_get_pml4e(void);
 bool smp_supported(void);
 bool default_supported(void);
+bool vgif_supported(void);
 void default_prepare(struct svm_test *test);
 void default_prepare_gif_clear(struct svm_test *test);
 bool default_finished(struct svm_test *test);
x86/svm_tests.c +70 -0

82 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw

Nit, s/Added/Add in the shortlog

Quoted 6 lines · Lara Lazier

When VGIF is enabled STGI executed in guest mode
sets bit 9, while CLGI clears bit 9 in the int_ctl (offset 60h) of the VMCB.

---

...

Quoted 4 lines
x86/svm_tests.c +0 -0
@@ -2772,6 +2772,73 @@ static void svm_vmload_vmsave(void)
 	vmcb->control.intercept = intercept_saved;

x86/svm_tests.c static void svm_vmload_vmsave(void) · line 2782

Quoted 10 lines
x86/svm_tests.c +8 -0
 }
 
+static void prepare_vgif_enabled(struct svm_test *test)
+{
+    default_prepare(test);
+}
+
+static void test_vgif(struct svm_test *test)
+{
+    asm volatile ("vmmcall\n\tstgi\n\tvmmcall\n\tclgi\n\tvmmcall\n\t");

While amusing, this isn't very readable :-) The SVM tests that use this for back-to-back VMMCALL are setting a bad example. The space between "volatile" and the opening "(" can go too.

	asm volatile("vmmcall\n\t"
		     "stgi\n\t"
		     "vmmcall\n\t"
		     "clgi\n\t"
		     "vmmcall\n\t");

x86/svm_tests.c static void svm_vmload_vmsave(void) · line 2783

Quoted 1 line
x86/svm_tests.c +1 -0
+

Unnecessary newline.

x86/svm_tests.c static void svm_vmload_vmsave(void) · line 2795

Quoted 12 lines
x86/svm_tests.c +12 -0
+}
+
+static bool vgif_finished(struct svm_test *test)
+{
+    switch (get_test_stage(test))
+    {
+    case 0:
+        if (vmcb->control.exit_code != SVM_EXIT_VMMCALL) {
+            report(false, "VMEXIT not due to vmmcall.");
+            return true;
+        }
+        vmcb->control.int_ctl |= V_GIF_ENABLED_MASK;

Setting and restoring a control flag should be done in setup/teardown, e.g. this approach will leave V_GIF_ENABLED_MASK set if a VMMCALL check fails.

x86/svm_tests.c static void svm_vmload_vmsave(void) · line 2807

Quoted 12 lines
x86/svm_tests.c +12 -0
+        vmcb->save.rip += 3;
+        inc_test_stage(test);
+        break;
+    case 1:
+        if (vmcb->control.exit_code != SVM_EXIT_VMMCALL) {
+            report(false, "VMEXIT not due to vmmcall.");
+            return true;
+        }
+        if (!(vmcb->control.int_ctl & V_GIF_MASK)) {
+            report(false, "Failed to set VGIF when executing STGI.");
+            vmcb->control.int_ctl &= ~V_GIF_ENABLED_MASK;
+            return true;

Are the VGIF checks really fatal? I.e. can we keep running of a STGI/CLGI test
fails? That would allow for slightly cleaner code.

Quoted 10 lines
x86/svm_tests.c +10 -0
+        }
+        report(true, "STGI set VGIF bit.");
+        vmcb->save.rip += 3;
+        inc_test_stage(test);
+        break;
+    case 2:
+        if (vmcb->control.exit_code != SVM_EXIT_VMMCALL) {
+            report(false, "VMEXIT not due to vmmcall.");
+            return true;
+        }

x86/svm_tests.c static void svm_vmload_vmsave(void) · line 2827

Quoted 10 lines
x86/svm_tests.c +10 -0
+        if (vmcb->control.int_ctl & V_GIF_MASK) {
+            report(false, "Failed to clear VGIF when executing CLGI.");
+            vmcb->control.int_ctl &= ~V_GIF_ENABLED_MASK;
+            return true;
+        }
+        report(true, "CLGI cleared VGIF bit.");
+        vmcb->save.rip += 3;
+        inc_test_stage(test);
+        vmcb->control.int_ctl &= ~V_GIF_ENABLED_MASK;
+        break;

Something like this is more reader friendly as the boilerplate is consoliated, abd the interesting test code is isolated in the switch statement.

	bool is_vmmcall_exit = (vmcb->control.exit_code == SVM_EXIT_VMMCALL);

	report(is_vmmcall_exit, ...);
	if (!is_vmmcall_exit)
		return true;

	switch (get_test_stage())
	{
	case 0:
		vmcb->control.int_ctl |= V_GIF_ENABLED_MASK;
		break;
	case 1:
		report(vmcb->control.int_ctl & V_GIF_MASK, ...);
		break;
	case 2:
		report(!(vmcb->control.int_ctl & V_GIF_MASK), ...);
		break;
	default:
		break;
	}

	vmcb->save.rip += 3;
	inc_test_stage(test);

	return get_test_stage() >= 3;
Quoted 4 lines
x86/svm_tests.c +4 -0
+    default:
+        return true;
+        break;
+    }