Skip to contentSkip to conversation

kvm-unit-tests: VMX: vmentry checks

RFC PATCH In review Arthur Chunqi Li 1 participant 4 messages Sep 13, 2013 KVM

lore
Share & tools
lore
#0 lore raw

cover kvm-unit-tests: VMX: vmentry checks

This series implement a framework to capture early exit in vmenter, means vmenter fails to next instruction instead of causing vmexit. Then all supported features referred in Intel SDM 26.1 and 26.2 are tested.

Some test cases are commented since they may cause fatal error of KVM, thus will crash test environment and affect the following tests. They are hoped to uncomment after related bugs are fixed.

Arthur Chunqi Li (2):
kvm-unit-tests: VMX: Add vmentry failed handler to framework
kvm-unit-tests: VMX: Add test cases for vmentry checks

 lib/x86/vm.h    |    3 +
 x86/vmx.c       |   34 +--
 x86/vmx.h       |   37 ++-
 x86/vmx_tests.c |  678 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 725 insertions(+), 27 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

RFC PATCH 1/2 kvm-unit-tests: VMX: Add vmentry failed handler to framework

Add vmentry failed handler to vmx framework to catch direct fail of vmentry. When vmlaunch/vmresume directly fail to the next instruction, a entry failed handler is used to handle this failure. Resume failure from entry failed handler will cause entry double fail and directly exit to L1.

---
 lib/x86/vm.h    |    3 +++
 x86/vmx.c       |   34 ++++++++++++++++++++--------------
 x86/vmx.h       |   15 +++++++++++++--
 x86/vmx_tests.c |   31 +++++++++++++++++++++----------
 4 files changed, 57 insertions(+), 26 deletions(-)
lib/x86/vm.h +3 -0
index 6e0ce2b..c8565b5 100644
@@ -19,7 +19,10 @@
 #define X86_CR0_PE      0x00000001
 #define X86_CR0_MP      0x00000002
 #define X86_CR0_TS      0x00000008
+#define X86_CR0_ET      0x00000010
 #define X86_CR0_WP      0x00010000
+#define X86_CR0_NW      0x20000000
+#define X86_CR0_CD      0x40000000
 #define X86_CR0_PG      0x80000000
 #define X86_CR4_VMXE   0x00000001
 #define X86_CR4_TSD     0x00000004
x86/vmx.c +20 -14
index 9db4ef4..6a2bf44 100644
@@ -44,14 +44,6 @@ void report(const char *name, int result)
 	}
 }
 
-static int make_vmcs_current(struct vmcs *vmcs)
-{
-	bool ret;
-
-	asm volatile ("vmptrld %1; setbe %0" : "=q" (ret) : "m" (vmcs) : "cc");
-	return ret;
-}
-
 /* entry_sysenter */
 asm(
 	".align	4, 0x90\n\t"
@@ -631,6 +623,7 @@ static int exit_handler()
 static int vmx_run()
 {
 	u32 ret = 0, fail = 0;
+	bool entry_double_fail = false;
 
 	while (1) {
 		asm volatile (
@@ -657,28 +650,41 @@ static int vmx_run()
 
 		);
 		if (fail)
-			ret = launched ? VMX_TEST_RESUME_ERR :
-				VMX_TEST_LAUNCH_ERR;
+			if (entry_double_fail)
+				ret = launched ? VMX_TEST_RESUME_ERR :
+					VMX_TEST_LAUNCH_ERR;
+			else {
+				ret = current->entry_failed_handler(launched);
+				if (ret == VMX_TEST_RESUME) {
+					entry_double_fail = true;
+					host_rflags &= ~(X86_EFLAGS_ZF |
+						X86_EFLAGS_CF);
+				}
+			}
 		else {
 			launched = 1;
+			entry_double_fail = false;
 			ret = exit_handler();
 		}
 		if (ret != VMX_TEST_RESUME)
 			break;
+		ret = fail = 0;
 	}
 	launched = 0;
 	switch (ret) {
 	case VMX_TEST_VMEXIT:
 		return 0;
 	case VMX_TEST_LAUNCH_ERR:
-		printf("%s : vmlaunch failed.\n", __func__);
+		printf("%s : vmlaunch failed, entry_double_fail=%d.\n",
+			__func__, entry_double_fail);
 		if ((!(host_rflags & X86_EFLAGS_CF) && !(host_rflags & X86_EFLAGS_ZF))
 			|| ((host_rflags & X86_EFLAGS_CF) && (host_rflags & X86_EFLAGS_ZF)))
 			printf("\tvmlaunch set wrong flags\n");
 		report("test vmlaunch", 0);
 		break;
 	case VMX_TEST_RESUME_ERR:
-		printf("%s : vmresume failed.\n", __func__);
+		printf("%s : vmresume failed, entry_double_fail=%d.\n",
+			__func__, entry_double_fail);
 		if ((!(host_rflags & X86_EFLAGS_CF) && !(host_rflags & X86_EFLAGS_ZF))
 			|| ((host_rflags & X86_EFLAGS_CF) && (host_rflags & X86_EFLAGS_ZF)))
 			printf("\tvmresume set wrong flags\n");
@@ -700,12 +706,12 @@ static int test_run(struct vmx_test *test)
 		return 1;
 	}
 	init_vmcs(&(test->vmcs));
+	current = test;
 	/* Directly call test->init is ok here, init_vmcs has done
 	   vmcs init, vmclear and vmptrld*/
 	if (test->init)
-		test->init(test->vmcs);
+		test->init();
 	test->exits = 0;
-	current = test;
 	regs = test->guest_regs;
 	vmcs_write(GUEST_RFLAGS, regs.rflags | 0x2);
 	launched = 0;
x86/vmx.h +13 -2
index dc1ebdf..469b4dc 100644
@@ -4,7 +4,8 @@
 #include "libcflat.h"
 
 struct vmcs {
-	u32 revision_id; /* vmcs revision identifier */
+	u32 revision_id:31, /* vmcs revision identifier */
+		shadow:1; /* shadow-VMCS indicator */
 	u32 abort; /* VMX-abort indicator */
 	/* VMCS data */
 	char data[0];
@@ -32,10 +33,11 @@ struct regs {
 
 struct vmx_test {
 	const char *name;
-	void (*init)(struct vmcs *vmcs);
+	void (*init)();
 	void (*guest_main)();
 	int (*exit_handler)();
 	void (*syscall_handler)(u64 syscall_no);
+	int (*entry_failed_handler)();
 	struct regs guest_regs;
 	struct vmcs *vmcs;
 	int exits;
@@ -378,6 +380,7 @@ enum Ctrl1 {
 	CPU_URG			= 1ul << 7,
 	CPU_WBINVD		= 1ul << 6,
 	CPU_RDRAND		= 1ul << 11,
+	CPU_SHADOW		= 1ul << 14,
 };
 
 #define SAVE_GPR				\
@@ -512,6 +515,14 @@ extern union vmx_ctrl_exit ctrl_exit_rev;
 extern union vmx_ctrl_ent ctrl_enter_rev;
 extern union vmx_ept_vpid  ept_vpid;
 
+static int make_vmcs_current(struct vmcs *vmcs)
+{
+	bool ret;
+
+	asm volatile ("vmptrld %1; setbe %0" : "=q" (ret) : "m" (vmcs) : "cc");
+	return ret;
+}
+
 static inline int vmcs_clear(struct vmcs *vmcs)
 {
 	bool ret;
x86/vmx_tests.c +21 -10
index 0759e10..e95e6b8 100644
@@ -73,6 +73,12 @@ void basic_syscall_handler(u64 syscall_no)
 {
 }
 
+int basic_entry_failed_handler()
+{
+	return launched ? VMX_TEST_RESUME_ERR :
+			VMX_TEST_LAUNCH_ERR;
+}
+
 void vmenter_main()
 {
 	u64 rax;
@@ -1111,22 +1117,27 @@ static int ept_exit_handler()
    basic_* just implement some basic functions */
 struct vmx_test vmx_tests[] = {
 	{ "null", basic_init, basic_guest_main, basic_exit_handler,
-		basic_syscall_handler, {0} },
+		basic_syscall_handler, basic_entry_failed_handler, {0} },
 	{ "vmenter", basic_init, vmenter_main, vmenter_exit_handler,
-		basic_syscall_handler, {0} },
+		basic_syscall_handler, basic_entry_failed_handler, {0} },
 	{ "preemption timer", preemption_timer_init, preemption_timer_main,
-		preemption_timer_exit_handler, basic_syscall_handler, {0} },
+		preemption_timer_exit_handler, basic_syscall_handler,
+		basic_entry_failed_handler, {0} },
 	{ "control field PAT", test_ctrl_pat_init, test_ctrl_pat_main,
-		test_ctrl_pat_exit_handler, basic_syscall_handler, {0} },
+		test_ctrl_pat_exit_handler, basic_syscall_handler,
+		basic_entry_failed_handler, {0} },
 	{ "control field EFER", test_ctrl_efer_init, test_ctrl_efer_main,
-		test_ctrl_efer_exit_handler, basic_syscall_handler, {0} },
+		test_ctrl_efer_exit_handler, basic_syscall_handler,
+		basic_entry_failed_handler, {0} },
 	{ "CR shadowing", basic_init, cr_shadowing_main,
-		cr_shadowing_exit_handler, basic_syscall_handler, {0} },
+		cr_shadowing_exit_handler, basic_syscall_handler,
+		basic_entry_failed_handler, {0} },
 	{ "I/O bitmap", iobmp_init, iobmp_main, iobmp_exit_handler,
-		basic_syscall_handler, {0} },
+		basic_syscall_handler, basic_entry_failed_handler, {0} },
 	{ "instruction intercept", insn_intercept_init, insn_intercept_main,
-		insn_intercept_exit_handler, basic_syscall_handler, {0} },
+		insn_intercept_exit_handler, basic_syscall_handler,
+		basic_entry_failed_handler, {0} },
 	{ "EPT framework", ept_init, ept_main, ept_exit_handler,
-		basic_syscall_handler, {0} },
-	{ NULL, NULL, NULL, NULL, NULL, {0} },
+		basic_syscall_handler, basic_entry_failed_handler, {0} },
+	{ NULL, NULL, NULL, NULL, NULL, NULL, {0} },
 };
Signature · 1 line
1.7.9.5
#2 lore raw

RFC PATCH 2/2 kvm-unit-tests: VMX: Add test cases for vmentry checks

In reply to Arthur Chunqi Li

This patch design a framwork to check vmentry fields, then test all supported features in Intel SDM 26.1 and 26.2. Unsupported features are not tested, but listed in the code.

To add new tests for vmentry checks, just write functions for test initialization and add related item in vmentry_cases.

---
 x86/vmx.h       |   22 +-
 x86/vmx_tests.c |  647 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 668 insertions(+), 1 deletion(-)
x86/vmx.h +21 -1
index 469b4dc..aeee602 100644
@@ -344,6 +344,8 @@ enum Ctrl_exi {
 
 enum Ctrl_ent {
 	ENT_GUEST_64            = 1UL << 9,
+	ENT_ENT_SMM		= 1UL << 10,
+	ENT_DEATV_DM		= 1UL << 11,
 	ENT_LOAD_PAT		= 1UL << 14,
 	ENT_LOAD_EFER           = 1UL << 15,
 };
@@ -375,10 +377,13 @@ enum Ctrl0 {
 };
 
 enum Ctrl1 {
+	CPU_VIRT_APIC		= 1ul << 0,
 	CPU_EPT			= 1ul << 1,
+	CPU_VIRT_X2APIC		= 1ul << 4,
 	CPU_VPID		= 1ul << 5,
-	CPU_URG			= 1ul << 7,
 	CPU_WBINVD		= 1ul << 6,
+	CPU_URG			= 1ul << 7,
+	CPU_VIRT_INTR		= 1ul << 9,
 	CPU_RDRAND		= 1ul << 11,
 	CPU_SHADOW		= 1ul << 14,
 };
@@ -453,6 +458,7 @@ enum Ctrl1 {
 #define HYPERCALL_VMEXIT	0x1
 
 #define EPTP_PG_WALK_LEN_SHIFT	3ul
+#define EPTP_PG_WALK_LEN_MASK	0x31
 #define EPTP_AD_FLAG			(1ul << 6)
 
 #define EPT_MEM_TYPE_UC	0ul
@@ -460,6 +466,7 @@ enum Ctrl1 {
 #define EPT_MEM_TYPE_WT	4ul
 #define EPT_MEM_TYPE_WP	5ul
 #define EPT_MEM_TYPE_WB	6ul
+#define EPT_MEM_TYPE_MASK 0x7
 
 #define EPT_RA			1ul
 #define EPT_WA			2ul
@@ -506,6 +513,19 @@ enum Ctrl1 {
 #define INVEPT_SINGLE		1
 #define INVEPT_GLOBAL		2
 
+#define INTR_INFO_TYPE_MASK		0x00000700
+#define INTR_INFO_TYPE_SHIFT		8
+#define INTR_INFO_TYPE_EXT		0
+#define INTR_INFO_TYPE_REV		1
+#define INTR_INFO_TYPE_NMI		2
+#define INTR_INFO_TYPE_HARD_EXP		3
+#define INTR_INFO_TYPE_SOFT_INTR	4
+#define INTR_INFO_TYPE_PSE		5
+#define INTR_INFO_TYPE_SOFT_EXP		6
+#define INTR_INFO_TYPE_OTHER		7
+#define INTR_INFO_DELIVER_ERR		0x00000800
+#define INTR_INFO_VALID			0x80000000
+
 extern struct regs regs;
 
 extern union vmx_basic basic;
x86/vmx_tests.c +647 -0

664 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#3 lore raw
In reply to Arthur Chunqi Li

Hi Gleb, Paolo and Jan,

There are indeed many checks that fail to check in vmentry, the result in my computer is here:

Test suite : vmentry check
PASS: No current VMCS vmenter
PASS: VMLAUNCH with state not clear
PASS: VMRESUME with state not launched
PASS: Reserved bits in PIN_CONTROLS field
PASS: Reserved bits in primary CPU CONTROLS field
PASS: Reserved bits in secondary CPU CONTROLS field
FAIL: CR3 target count greater than 4
FAIL: I/O bitmap address invalid (aligned)
FAIL: I/O bitmap address invalid (exceed)
PASS: MSR bitmap address invalid (aligned)
FAIL: MSR bitmap address invalid (exceed)
FAIL: Consistency of NMI exiting and virtual NMIs
PASS: APIC-accesses address invalid (aligned)
FAIL: APIC-accesses address invalid (exceed)
FAIL: EPTP memory type
FAIL: EPTP page walk length
FAIL: EPTP page reserved bits (11:7)
PASS: Reserved bits in EXI_CONTROLS field
FAIL: Consistency of VMX-preemption timer (activate and save)
PASS: Reserved bits in ENT_CONTROLS field
PASS: Entry to SMM with processor not in SMM
PASS: Deactivate dual-monitor treatment with processor not in SMM
PASS: Invalid bits in host CR0
FAIL: Invalid bits in host CR4
FAIL: Invalid bits in host CR3
FAIL: Invalid host sysenter esp addr
FAIL: Invalid host sysenter eip addr
FAIL: Invalid CS selector field - TI flag
FAIL: Invalid TR selector field - TI flag
FAIL: Invalid CS selector field - 0000H
FAIL: Invalid TR selector field - 0000H
FAIL: Invalid base address of FS
FAIL: Invalid base address of GS
FAIL: Invalid base address of GDTR
FAIL: Invalid base address of IDTR
FAIL: Invalid base address of TR
FAIL: Consistency of EXI_HOST_64 and CR4.PAE

SUMMARY: 90 tests, 24 failures

Besides, all commented cases also fail, they are:
EPTP page reserved bits (63:N)
Invalid host PAT
Invalid host EFER - bits reserved
Invalid host EFER - LMA & LME
Invalid CS selector field - RPL
Invalid TR selector field - RPL

You can find detailed description of these cases in Intel SDM 26.1 and 26.2.

Arthur

Quoted 21 lines · Arthur Chunqi Li

This series implement a framework to capture early exit in vmenter, means vmenter fails to next instruction instead of causing vmexit. Then all supported features referred in Intel SDM 26.1 and 26.2 are tested.

Some test cases are commented since they may cause fatal error of KVM, thus will crash test environment and affect the following tests. They are hoped to uncomment after related bugs are fixed.

Arthur Chunqi Li (2):
kvm-unit-tests: VMX: Add vmentry failed handler to framework
kvm-unit-tests: VMX: Add test cases for vmentry checks

 lib/x86/vm.h    |    3 +
 x86/vmx.c       |   34 +--
 x86/vmx.h       |   37 ++-
 x86/vmx_tests.c |  678 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 725 insertions(+), 27 deletions(-)

--
1.7.9.5